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