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