1 /* Header for charset handler.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007, 2008, 2009, 2010
6 National Institute of Advanced Industrial Science and Technology (AIST)
7 Registration Number H14PRO021
10 National Institute of Advanced Industrial Science and Technology (AIST)
11 Registration Number H13PRO009
13 This file is part of GNU Emacs.
15 GNU Emacs is free software: you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation, either version 3 of the License, or
18 (at your option) any later version.
20 GNU Emacs is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
28 #ifndef EMACS_CHARSET_H
29 #define EMACS_CHARSET_H
31 /* Index to arguments of Fdefine_charset_internal. */
33 enum define_charset_arg_index
36 charset_arg_dimension
,
37 charset_arg_code_space
,
40 charset_arg_iso_final
,
41 charset_arg_iso_revision
,
42 charset_arg_emacs_mule_id
,
43 charset_arg_ascii_compatible_p
,
44 charset_arg_supplementary_p
,
45 charset_arg_invalid_code
,
46 charset_arg_code_offset
,
50 charset_arg_unify_map
,
56 /* Indices to charset attributes vector. */
58 enum charset_attr_index
60 /* ID number of the charset. */
63 /* Name of the charset (symbol). */
66 /* Property list of the charset. */
69 /* If the method of the charset is `MAP', the value is a mapping
70 vector or a file name that contains mapping vector. Otherwise,
74 /* If the method of the charset is `MAP', the value is a vector
75 that maps code points of the charset to characters. The vector
76 is indexed by a character index. A character index is
77 calculated from a code point and the code-space table of the
81 /* If the method of the charset is `MAP', the value is a
82 char-table that maps characters of the charset to code
86 /* If the method of the charset is `SUBSET', the value is a vector
89 [ CHARSET-ID MIN-CODE MAX-CODE OFFSET ]
91 CHARSET-ID is an ID number of a parent charset. MIN-CODE and
92 MAX-CODE specify the range of characters inherited from the
93 parent. OFFSET is an integer value to add to a code point of
94 the parent charset to get the corresponding code point of this
98 /* If the method of the charset is `SUPERSET', the value is a list
99 whose elements have this form:
101 (CHARSET-ID . OFFSET)
103 CHARSET-IDs are ID numbers of parent charsets. OFFSET is an
104 integer value to add to a code point of the parent charset to
105 get the corresponding code point of this charset. */
108 /* The value is a mapping vector or a file name that contains the
109 mapping. This defines how characters in the charset should be
110 unified with Unicode. The value of the member
111 `charset_deunifier' is created from this information. */
114 /* If characters in the charset must be unified Unicode, the value
115 is a char table that maps a unified Unicode character code to
116 the non-unified character code in the charset. */
119 /* The length of the charset attribute vector. */
123 /* Methods for converting code points and characters of charsets. */
127 /* For a charset of this method, a character code is calculated
128 from a character index (which is calculated from a code point)
129 simply by adding an offset value. */
130 CHARSET_METHOD_OFFSET
,
132 /* For a charset of this method, a decoder vector and an encoder
133 char-table is used for code point <-> character code
137 /* A charset of this method is a subset of another charset. */
138 CHARSET_METHOD_SUBSET
,
140 /* A charset of this method is a superset of other charsets. */
141 CHARSET_METHOD_SUPERSET
146 /* Index to charset_table. */
149 /* Index to Vcharset_hash_table. */
152 /* Dimension of the charset: 1, 2, 3, or 4. */
155 /* Byte code range of each dimension. <code_space>[4N] is a mininum
156 byte code of the (N+1)th dimension, <code_space>[4N+1] is a
157 maximum byte code of the (N+1)th dimension, <code_space>[4N+2] is
158 (<code_space>[4N+1] - <code_space>[4N] + 1), <code_space>[4N+3]
159 is a number of characters containd in the first to (N+1)th
160 dismesions. We get `char-index' of a `code-point' from this
164 /* If B is a byte of Nth dimension of a code-point, the (N-1)th bit
165 of code_space_mask[B] is set. This array is used to quickly
166 check if a code-point is in a valid range. */
167 unsigned char *code_space_mask
;
169 /* 1 if there's no gap in code-points. */
172 /* If the charset is treated as 94-chars in ISO-2022, the value is 0.
173 If the charset is treated as 96-chars in ISO-2022, the value is 1. */
176 /* ISO final byte of the charset: 48..127. It may be -1 if the
177 charset doesn't conform to ISO-2022. */
180 /* ISO revision number of the charset. */
183 /* If the charset is identical to what supported by Emacs 21 and the
184 priors, the identification number of the charset used in those
185 version. Otherwise, -1. */
188 /* Nonzero if the charset is compatible with ASCII. */
189 int ascii_compatible_p
;
191 /* Nonzero if the charset is supplementary. */
194 /* Nonzero if all the code points are representable by Lisp_Int. */
197 /* The method for encoding/decoding characters of the charset. */
198 enum charset_method method
;
200 /* Mininum and Maximum code points of the charset. */
201 unsigned min_code
, max_code
;
203 /* Offset value used by macros CODE_POINT_TO_INDEX and
204 INDEX_TO_CODE_POINT. . */
205 unsigned char_index_offset
;
207 /* Mininum and Maximum character codes of the charset. If the
208 charset is compatible with ASCII, min_char is a minimum non-ASCII
209 character of the charset. If the method of charset is
210 CHARSET_METHOD_OFFSET, even if the charset is unified, min_char
211 and max_char doesn't change. */
212 int min_char
, max_char
;
214 /* The code returned by ENCODE_CHAR if a character is not encodable
216 unsigned invalid_code
;
218 /* If the method of the charset is CHARSET_METHOD_MAP, this is a
219 table of bits used to quickly and roughly guess if a character
220 belongs to the charset.
222 The first 64 elements are 512 bits for characters less than
223 0x10000. Each bit corresponds to 128-character block. The last
224 126 elements are 1008 bits for the greater characters
225 (0x10000..0x3FFFFF). Each bit corresponds to 4096-character
228 If a bit is 1, at least one character in the corresponding block is
230 unsigned char fast_map
[190];
232 /* Offset value to calculate a character code from code-point, and
239 /* Hash table of charset symbols vs. the correponding attribute
241 extern Lisp_Object Vcharset_hash_table
;
243 /* Table of struct charset. */
244 extern struct charset
*charset_table
;
246 #define CHARSET_FROM_ID(id) (charset_table + (id))
248 extern Lisp_Object Vcharset_ordered_list
;
249 extern Lisp_Object Vcharset_non_preferred_head
;
251 /* Incremented everytime we change the priority of charsets. */
252 extern unsigned short charset_ordered_list_tick
;
254 extern Lisp_Object Vcharset_list
;
255 extern Lisp_Object Viso_2022_charset_list
;
256 extern Lisp_Object Vemacs_mule_charset_list
;
258 extern struct charset
*emacs_mule_charset
[256];
260 extern Lisp_Object Vcurrent_iso639_language
;
262 /* Macros to access information about charset. */
264 /* Return the attribute vector of charset whose symbol is SYMBOL. */
265 #define CHARSET_SYMBOL_ATTRIBUTES(symbol) \
266 Fgethash ((symbol), Vcharset_hash_table, Qnil)
268 #define CHARSET_ATTR_ID(attrs) AREF ((attrs), charset_id)
269 #define CHARSET_ATTR_NAME(attrs) AREF ((attrs), charset_name)
270 #define CHARSET_ATTR_PLIST(attrs) AREF ((attrs), charset_plist)
271 #define CHARSET_ATTR_MAP(attrs) AREF ((attrs), charset_map)
272 #define CHARSET_ATTR_DECODER(attrs) AREF ((attrs), charset_decoder)
273 #define CHARSET_ATTR_ENCODER(attrs) AREF ((attrs), charset_encoder)
274 #define CHARSET_ATTR_SUBSET(attrs) AREF ((attrs), charset_subset)
275 #define CHARSET_ATTR_SUPERSET(attrs) AREF ((attrs), charset_superset)
276 #define CHARSET_ATTR_UNIFY_MAP(attrs) AREF ((attrs), charset_unify_map)
277 #define CHARSET_ATTR_DEUNIFIER(attrs) AREF ((attrs), charset_deunifier)
279 #define CHARSET_SYMBOL_ID(symbol) \
280 CHARSET_ATTR_ID (CHARSET_SYMBOL_ATTRIBUTES (symbol))
282 /* Return an index to Vcharset_hash_table of the charset whose symbol
284 #define CHARSET_SYMBOL_HASH_INDEX(symbol) \
285 hash_lookup (XHASH_TABLE (Vcharset_hash_table), symbol, NULL)
287 /* Return the attribute vector of CHARSET. */
288 #define CHARSET_ATTRIBUTES(charset) \
289 (HASH_VALUE (XHASH_TABLE (Vcharset_hash_table), (charset)->hash_index))
291 #define CHARSET_ID(charset) ((charset)->id)
292 #define CHARSET_HASH_INDEX(charset) ((charset)->hash_index)
293 #define CHARSET_DIMENSION(charset) ((charset)->dimension)
294 #define CHARSET_CODE_SPACE(charset) ((charset)->code_space)
295 #define CHARSET_CODE_LINEAR_P(charset) ((charset)->code_linear_p)
296 #define CHARSET_ISO_CHARS_96(charset) ((charset)->iso_chars_96)
297 #define CHARSET_ISO_FINAL(charset) ((charset)->iso_final)
298 #define CHARSET_ISO_PLANE(charset) ((charset)->iso_plane)
299 #define CHARSET_ISO_REVISION(charset) ((charset)->iso_revision)
300 #define CHARSET_EMACS_MULE_ID(charset) ((charset)->emacs_mule_id)
301 #define CHARSET_ASCII_COMPATIBLE_P(charset) ((charset)->ascii_compatible_p)
302 #define CHARSET_COMPACT_CODES_P(charset) ((charset)->compact_codes_p)
303 #define CHARSET_METHOD(charset) ((charset)->method)
304 #define CHARSET_MIN_CODE(charset) ((charset)->min_code)
305 #define CHARSET_MAX_CODE(charset) ((charset)->max_code)
306 #define CHARSET_INVALID_CODE(charset) ((charset)->invalid_code)
307 #define CHARSET_MIN_CHAR(charset) ((charset)->min_char)
308 #define CHARSET_MAX_CHAR(charset) ((charset)->max_char)
309 #define CHARSET_CODE_OFFSET(charset) ((charset)->code_offset)
310 #define CHARSET_UNIFIED_P(charset) ((charset)->unified_p)
312 #define CHARSET_NAME(charset) \
313 (CHARSET_ATTR_NAME (CHARSET_ATTRIBUTES (charset)))
314 #define CHARSET_MAP(charset) \
315 (CHARSET_ATTR_MAP (CHARSET_ATTRIBUTES (charset)))
316 #define CHARSET_DECODER(charset) \
317 (CHARSET_ATTR_DECODER (CHARSET_ATTRIBUTES (charset)))
318 #define CHARSET_ENCODER(charset) \
319 (CHARSET_ATTR_ENCODER (CHARSET_ATTRIBUTES (charset)))
320 #define CHARSET_SUBSET(charset) \
321 (CHARSET_ATTR_SUBSET (CHARSET_ATTRIBUTES (charset)))
322 #define CHARSET_SUPERSET(charset) \
323 (CHARSET_ATTR_SUPERSET (CHARSET_ATTRIBUTES (charset)))
324 #define CHARSET_UNIFY_MAP(charset) \
325 (CHARSET_ATTR_UNIFY_MAP (CHARSET_ATTRIBUTES (charset)))
326 #define CHARSET_DEUNIFIER(charset) \
327 (CHARSET_ATTR_DEUNIFIER (CHARSET_ATTRIBUTES (charset)))
330 /* Nonzero if OBJ is a valid charset symbol. */
331 #define CHARSETP(obj) (CHARSET_SYMBOL_HASH_INDEX (obj) >= 0)
333 /* Check if X is a valid charset symbol. If not, signal an error. */
334 #define CHECK_CHARSET(x) \
336 if (! SYMBOLP (x) || CHARSET_SYMBOL_HASH_INDEX (x) < 0) \
337 wrong_type_argument (Qcharsetp, (x)); \
341 /* Check if X is a valid charset symbol. If valid, set ID to the id
342 number of the charset. Otherwise, signal an error. */
343 #define CHECK_CHARSET_GET_ID(x, id) \
347 if (! SYMBOLP (x) || (idx = CHARSET_SYMBOL_HASH_INDEX (x)) < 0) \
348 wrong_type_argument (Qcharsetp, (x)); \
349 id = XINT (AREF (HASH_VALUE (XHASH_TABLE (Vcharset_hash_table), idx), \
354 /* Check if X is a valid charset symbol. If valid, set ATTR to the
355 attr vector of the charset. Otherwise, signal an error. */
356 #define CHECK_CHARSET_GET_ATTR(x, attr) \
358 if (!SYMBOLP (x) || NILP (attr = CHARSET_SYMBOL_ATTRIBUTES (x))) \
359 wrong_type_argument (Qcharsetp, (x)); \
363 #define CHECK_CHARSET_GET_CHARSET(x, charset) \
366 CHECK_CHARSET_GET_ID (x, id); \
367 charset = CHARSET_FROM_ID (id); \
371 /* Lookup Vcharset_ordered_list and return the first charset that
372 contains the character C. */
373 #define CHAR_CHARSET(c) \
374 ((c) < 0x80 ? CHARSET_FROM_ID (charset_ascii) \
375 : char_charset ((c), Qnil, NULL))
378 /* Char-table of charset-sets. Each element is a bool vector indexed
380 extern Lisp_Object Vchar_charset_set
;
382 /* Charset-bag of character C. */
383 #define CHAR_CHARSET_SET(c) \
384 CHAR_TABLE_REF (Vchar_charset_set, c)
386 /* Check if two characters C1 and C2 belong to the same charset. */
387 #define SAME_CHARSET_P(c1, c2) \
388 intersection_p (CHAR_CHARSET_SET (c1), CHAR_CHARSET_SET (c2))
393 /* Return a character correponding to the code-point CODE of CHARSET.
394 Try some optimization before calling decode_char. */
396 #define DECODE_CHAR(charset, code) \
397 ((ASCII_BYTE_P (code) && (charset)->ascii_compatible_p) \
399 : ((code) < (charset)->min_code || (code) > (charset)->max_code) \
401 : (charset)->unified_p \
402 ? decode_char ((charset), (code)) \
403 : (charset)->method == CHARSET_METHOD_OFFSET \
404 ? ((charset)->code_linear_p \
405 ? (code) - (charset)->min_code + (charset)->code_offset \
406 : decode_char ((charset), (code))) \
407 : (charset)->method == CHARSET_METHOD_MAP \
408 ? (((charset)->code_linear_p \
409 && VECTORP (CHARSET_DECODER (charset))) \
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 && CHAR_TABLE_P (CHARSET_ENCODER (charset))) \
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)))
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
;
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)))
465 #define ISO_MAX_DIMENSION 3
466 #define ISO_MAX_CHARS 2
467 #define ISO_MAX_FINAL 0x80 /* only 0x30..0xFF are used */
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
];
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)]
479 /* Nonzero if the charset who has FAST_MAP may contain C. */
480 #define CHARSET_FAST_MAP_REF(c, fast_map) \
482 ? fast_map[(c) >> 10] & (1 << (((c) >> 7) & 7)) \
483 : fast_map[((c) >> 15) + 62] & (1 << (((c) >> 12) & 7)))
485 #define CHARSET_FAST_MAP_SET(c, fast_map) \
488 (fast_map)[(c) >> 10] |= 1 << (((c) >> 7) & 7); \
490 (fast_map)[((c) >> 15) + 62] |= 1 << (((c) >> 12) & 7); \
495 /* 1 if 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 && CHAR_TABLE_P (CHARSET_ENCODER (charset))) \
508 ? ! NILP (CHAR_TABLE_REF (CHARSET_ENCODER (charset), (c))) \
509 : encode_char ((charset), (c)) != (charset)->invalid_code))))
512 /* Special macros for emacs-mule encoding. */
514 /* Leading-code followed by extended leading-code. DIMENSION/COLUMN */
515 #define EMACS_MULE_LEADING_CODE_PRIVATE_11 0x9A /* 1/1 */
516 #define EMACS_MULE_LEADING_CODE_PRIVATE_12 0x9B /* 1/2 */
517 #define EMACS_MULE_LEADING_CODE_PRIVATE_21 0x9C /* 2/2 */
518 #define EMACS_MULE_LEADING_CODE_PRIVATE_22 0x9D /* 2/2 */
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
;
531 extern int charset_ksc5601
;
533 extern int charset_unibyte
;
535 extern struct charset
*char_charset (int, Lisp_Object
, unsigned *);
536 extern Lisp_Object
charset_attributes (int);
538 extern int maybe_unify_char (int, Lisp_Object
);
539 extern int decode_char (struct charset
*, unsigned);
540 extern unsigned encode_char (struct charset
*, int);
541 extern int string_xstring_p (Lisp_Object
);
543 extern void map_charset_chars (void (*) (Lisp_Object
, Lisp_Object
),
544 Lisp_Object
, Lisp_Object
,
545 struct charset
*, unsigned, unsigned);
547 EXFUN (Funify_charset
, 3);
549 #endif /* EMACS_CHARSET_H */
551 /* arch-tag: 3b96db55-4961-481d-ac3e-219f46a2b3aa
552 (do not change this comment) */