(byte_table_same_value_p): Use `internal_equal' instead of `EQ'.
[chise/xemacs-chise.git-] / src / char-ucs.h
1 /* Header for UCS-4 character representation.
2    Copyright (C) 1999,2000 MORIOKA Tomohiko
3
4 This file is part of XEmacs.
5
6 XEmacs is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
10
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with XEmacs; see the file COPYING.  If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21 #ifndef INCLUDED_char_ucs_h_
22 #define INCLUDED_char_ucs_h_
23
24 #define valid_char_p(ch) 1
25
26 #define CHAR_ASCII_P(ch) ((ch) <= 0x7F)
27
28 \f
29 struct Lisp_Uint8_Byte_Table
30 {
31   struct lcrecord_header header;
32
33   unsigned char property[256];
34 };
35 typedef struct Lisp_Uint8_Byte_Table Lisp_Uint8_Byte_Table;
36
37 DECLARE_LRECORD (uint8_byte_table, Lisp_Uint8_Byte_Table);
38 #define XUINT8_BYTE_TABLE(x) \
39    XRECORD (x, uint8_byte_table, Lisp_Uint8_Byte_Table)
40 #define XSETUINT8_BYTE_TABLE(x, p) XSETRECORD (x, p, uint8_byte_table)
41 #define UINT8_BYTE_TABLE_P(x) RECORDP (x, uint8_byte_table)
42 #define GC_UINT8_BYTE_TABLE_P(x) GC_RECORDP (x, uint8_byte_table)
43 /* #define CHECK_UINT8_BYTE_TABLE(x) CHECK_RECORD (x, uint8_byte_table)
44    char table entries should never escape to Lisp */
45
46
47 struct Lisp_Uint16_Byte_Table
48 {
49   struct lcrecord_header header;
50
51   unsigned short property[256];
52 };
53 typedef struct Lisp_Uint16_Byte_Table Lisp_Uint16_Byte_Table;
54
55 DECLARE_LRECORD (uint16_byte_table, Lisp_Uint16_Byte_Table);
56 #define XUINT16_BYTE_TABLE(x) \
57    XRECORD (x, uint16_byte_table, Lisp_Uint16_Byte_Table)
58 #define XSETUINT16_BYTE_TABLE(x, p) XSETRECORD (x, p, uint16_byte_table)
59 #define UINT16_BYTE_TABLE_P(x) RECORDP (x, uint16_byte_table)
60 #define GC_UINT16_BYTE_TABLE_P(x) GC_RECORDP (x, uint16_byte_table)
61 /* #define CHECK_UINT16_BYTE_TABLE(x) CHECK_RECORD (x, uint16_byte_table)
62    char table entries should never escape to Lisp */
63
64
65 struct Lisp_Byte_Table
66 {
67   struct lcrecord_header header;
68
69   Lisp_Object property[256];
70 };
71 typedef struct Lisp_Byte_Table Lisp_Byte_Table;
72
73 DECLARE_LRECORD (byte_table, Lisp_Byte_Table);
74 #define XBYTE_TABLE(x) XRECORD (x, byte_table, Lisp_Byte_Table)
75 #define XSETBYTE_TABLE(x, p) XSETRECORD (x, p, byte_table)
76 #define BYTE_TABLE_P(x) RECORDP (x, byte_table)
77 #define GC_BYTE_TABLE_P(x) GC_RECORDP (x, byte_table)
78 /* #define CHECK_BYTE_TABLE(x) CHECK_RECORD (x, byte_table)
79    char table entries should never escape to Lisp */
80
81
82 struct Lisp_Char_ID_Table
83 {
84   struct lcrecord_header header;
85
86   Lisp_Object table;
87 };
88 typedef struct Lisp_Char_ID_Table Lisp_Char_ID_Table;
89
90 DECLARE_LRECORD (char_id_table, Lisp_Char_ID_Table);
91 #define XCHAR_ID_TABLE(x) XRECORD (x, char_id_table, Lisp_Char_ID_Table)
92 #define XSETCHAR_ID_TABLE(x, p) XSETRECORD (x, p, char_id_table)
93 #define CHAR_ID_TABLE_P(x) RECORDP (x, char_id_table)
94 #define GC_CHAR_ID_TABLE_P(x) GC_RECORDP (x, char_id_table)
95 /* #define CHECK_CHAR_ID_TABLE(x) CHECK_RECORD (x, char_id_table)
96    char table entries should never escape to Lisp */
97
98
99 Lisp_Object get_char_id_table (Emchar ch, Lisp_Object table);
100
101
102 extern Lisp_Object Vcharset_mojikyo;
103 extern Lisp_Object Vcharset_latin_jisx0201;
104
105 \f
106 /************************************************************************/
107 /*                    Definition of leading bytes                       */
108 /************************************************************************/
109
110 typedef short Charset_ID;
111
112 #define MIN_LEADING_BYTE                -0x60
113 #define CHARSET_ID_OFFSET               0x00
114
115 /* ISO/IEC 10646 */
116 #define LEADING_BYTE_UCS                (CHARSET_ID_OFFSET - 1)
117
118 /* represent normal 80-9F */
119 #define LEADING_BYTE_CONTROL_1          (CHARSET_ID_OFFSET - 2)
120
121 /* ISO/IEC 10646 BMP */
122 #define LEADING_BYTE_UCS_BMP            (CHARSET_ID_OFFSET - 3)
123
124 /* VISCII 1.1 */
125 #define LEADING_BYTE_LATIN_VISCII       (CHARSET_ID_OFFSET - 4)
126
127 /* MULE VISCII-LOWER                    (CHARSET_ID_OFFSET_96 + '1') */
128 #define LEADING_BYTE_LATIN_VISCII_LOWER (CHARSET_ID_OFFSET - 5)
129
130 /* MULE VISCII-UPPER                    (CHARSET_ID_OFFSET_96 + '2') */
131 #define LEADING_BYTE_LATIN_VISCII_UPPER (CHARSET_ID_OFFSET - 6)
132
133 /* Big5 Level 1                 2/4 2/{(8),9,10,11} 4/0 '0' */
134 #define LEADING_BYTE_CHINESE_BIG5_1     (CHARSET_ID_OFFSET - 7)
135
136 /* Big5 Level 2                 2/4 2/{(8),9,10,11} 4/0 '1' */
137 #define LEADING_BYTE_CHINESE_BIG5_2     (CHARSET_ID_OFFSET - 8)
138
139 #define LEADING_BYTE_ETHIOPIC_UCS       (CHARSET_ID_OFFSET - 9)
140
141 #define LEADING_BYTE_DAIKANWA           (CHARSET_ID_OFFSET - 10)
142 #define LEADING_BYTE_DAIKANWA_EKANJI    (CHARSET_ID_OFFSET - 11)
143 #define LEADING_BYTE_MOJIKYO            (CHARSET_ID_OFFSET - 12)
144
145 /* Japanese JIS X0208-1990      2/4 2/{(8),9,10,11} 4/2 (B) */
146 #define LEADING_BYTE_JAPANESE_JISX0208_1990 (CHARSET_ID_OFFSET - 13)
147
148 /* Konjaku-Mojikyo font (for pseudo-JIS X 0208 encoding) */
149 #define LEADING_BYTE_MOJIKYO_PJ_1       (CHARSET_ID_OFFSET - 14)
150 #define LEADING_BYTE_MOJIKYO_PJ_2       (CHARSET_ID_OFFSET - 15)
151 #define LEADING_BYTE_MOJIKYO_PJ_3       (CHARSET_ID_OFFSET - 16)
152 #define LEADING_BYTE_MOJIKYO_PJ_4       (CHARSET_ID_OFFSET - 17)
153 #define LEADING_BYTE_MOJIKYO_PJ_5       (CHARSET_ID_OFFSET - 18)
154 #define LEADING_BYTE_MOJIKYO_PJ_6       (CHARSET_ID_OFFSET - 19)
155 #define LEADING_BYTE_MOJIKYO_PJ_7       (CHARSET_ID_OFFSET - 20)
156 #define LEADING_BYTE_MOJIKYO_PJ_8       (CHARSET_ID_OFFSET - 21)
157 #define LEADING_BYTE_MOJIKYO_PJ_9       (CHARSET_ID_OFFSET - 22)
158 #define LEADING_BYTE_MOJIKYO_PJ_10      (CHARSET_ID_OFFSET - 23)
159 #define LEADING_BYTE_MOJIKYO_PJ_11      (CHARSET_ID_OFFSET - 24)
160 #define LEADING_BYTE_MOJIKYO_PJ_12      (CHARSET_ID_OFFSET - 25)
161 #define LEADING_BYTE_MOJIKYO_PJ_13      (CHARSET_ID_OFFSET - 26)
162 #define LEADING_BYTE_MOJIKYO_PJ_14      (CHARSET_ID_OFFSET - 27)
163 #define LEADING_BYTE_MOJIKYO_PJ_15      (CHARSET_ID_OFFSET - 28)
164 #define LEADING_BYTE_MOJIKYO_PJ_16      (CHARSET_ID_OFFSET - 29)
165 #define LEADING_BYTE_MOJIKYO_PJ_17      (CHARSET_ID_OFFSET - 30)
166 #define LEADING_BYTE_MOJIKYO_PJ_18      (CHARSET_ID_OFFSET - 31)
167 #define LEADING_BYTE_MOJIKYO_PJ_19      (CHARSET_ID_OFFSET - 32)
168 #define LEADING_BYTE_MOJIKYO_PJ_20      (CHARSET_ID_OFFSET - 33)
169 #define LEADING_BYTE_MOJIKYO_PJ_21      (CHARSET_ID_OFFSET - 34)
170
171 #define MIN_LEADING_BYTE_PRIVATE        MIN_LEADING_BYTE
172 #define MAX_LEADING_BYTE_PRIVATE        (CHARSET_ID_OFFSET - 35)
173
174
175 /* #define CHARSET_ID_OFFSET_94         (CHARSET_ID_OFFSET - '0') */
176
177 /* #define MIN_CHARSET_ID_PRIVATE_94    (CHARSET_ID_OFFSET_94 + '0') */
178 /* #define MAX_CHARSET_ID_PRIVATE_94    (CHARSET_ID_OFFSET_94 + '?') */
179
180 /* ISO 646 IRV */
181 #define LEADING_BYTE_ASCII                6 /* (CHARSET_ID_OFFSET_94 + 'B') */
182
183 /* Right half of JIS X0201-1976 */
184 #define LEADING_BYTE_KATAKANA_JISX0201   13 /* (CHARSET_ID_OFFSET_94 + 'I') */
185
186 /* Left  half of JIS X0201-1976 */
187 #define LEADING_BYTE_LATIN_JISX0201      14 /* (CHARSET_ID_OFFSET_94 + 'J') */
188
189
190 /* #define CHARSET_ID_OFFSET_96         (CHARSET_ID_OFFSET_94 + 80) */
191
192 /* Right half of ISO 8859-1 */
193 #define LEADING_BYTE_LATIN_ISO8859_1    100 /* (CHARSET_ID_OFFSET_96 + 'A') */
194
195 /* Right half of ISO 8859-2 */
196 #define LEADING_BYTE_LATIN_ISO8859_2    101 /* (CHARSET_ID_OFFSET_96 + 'B') */
197
198 /* Right half of ISO 8859-3 */
199 #define LEADING_BYTE_LATIN_ISO8859_3    109 /* (CHARSET_ID_OFFSET_96 + 'C') */
200
201 /* Right half of ISO 8859-4 */
202 #define LEADING_BYTE_LATIN_ISO8859_4    110 /* (CHARSET_ID_OFFSET_96 + 'D') */
203
204 /* Right half of ISO 8859-7 */
205 #define LEADING_BYTE_GREEK_ISO8859_7    126 /* (CHARSET_ID_OFFSET_96 + 'F') */
206
207 /* Right half of ISO 8859-6 */
208 #define LEADING_BYTE_ARABIC_ISO8859_6   127 /* (CHARSET_ID_OFFSET_96 + 'G') */
209
210 /* Right half of ISO 8859-8 */
211 #define LEADING_BYTE_HEBREW_ISO8859_8   138 /* (CHARSET_ID_OFFSET_96 + 'H') */
212
213 /* Right half of ISO 8859-5 */
214 #define LEADING_BYTE_CYRILLIC_ISO8859_5 144 /* (CHARSET_ID_OFFSET_96 + 'L') */
215
216 /* Right half of ISO 8859-9 */
217 #define LEADING_BYTE_LATIN_ISO8859_9    148 /* (CHARSET_ID_OFFSET_96 + 'M') */
218
219 /* TIS620-2533 */
220 #define LEADING_BYTE_THAI_TIS620        166 /* (CHARSET_ID_OFFSET_96 + 'T') */
221
222 /* Right-hand Part of the VSCII-2 (TCVN 5712:1983) */
223 #define LEADING_BYTE_LATIN_TCVN5712     180 /* F = 5/10 0x5A `Z' */
224
225
226 /* #define CHARSET_ID_OFFSET_94x94              (CHARSET_ID_OFFSET_96 + 80) */
227
228 /* #define MIN_LEADING_BYTE_PRIVATE_2   ('0' + CHARSET_ID_OFFSET_94x94) */
229 /* #define MAX_LEADING_BYTE_PRIVATE_2   ('?' + CHARSET_ID_OFFSET_94x94) */
230
231 /* Japanese JIS X0208-1978      2/4 2/{(8),9,10,11} 4/0 (@) */
232 #define LEADING_BYTE_JAPANESE_JISX0208_1978      42
233
234 /* Chinese Hanzi GB2312-1980    2/4 2/{(8),9,10,11} 4/1 (A) */
235 #define LEADING_BYTE_CHINESE_GB2312              58
236
237 /* Japanese JIS X0208-1983      2/4 2/{(8),9,10,11} 4/2 (B) */
238 #define LEADING_BYTE_JAPANESE_JISX0208           87
239
240 /* Hangul KS C5601-1987         2/4 2/{8,9,10,11} 4/3 (C) */
241 #define LEADING_BYTE_KOREAN_KSC5601             149
242
243 /* Japanese JIS X0212-1990      2/4 2/{8,9,10,11} 4/4 (D) */
244 #define LEADING_BYTE_JAPANESE_JISX0212          159
245
246 /* CCITT Extended GB            2/4 2/{8,9,10,11} 4/5 (E) */
247 #define LEADING_BYTE_CHINESE_CCITT_GB           165
248
249 /* Chinese CNS11643 Set 1       2/4 2/{8,9,10,11} 4/7 (G) */
250 #define LEADING_BYTE_CHINESE_CNS11643_1         171
251
252 /* Chinese CNS11643 Set 2       2/4 2/{8,9,10,11} 4/8 (H) */
253 #define LEADING_BYTE_CHINESE_CNS11643_2         172
254
255 /* Chinese CNS11643 Set 3       2/4 2/{8,9,10,11} 4/9 (I) */
256 #define LEADING_BYTE_CHINESE_CNS11643_3         183
257
258 /* Chinese CNS11643 Set 4       2/4 2/{8,9,10,11} 4/10 (J) */
259 #define LEADING_BYTE_CHINESE_CNS11643_4         184
260
261 /* Chinese CNS11643 Set 5       2/4 2/{8,9,10,11} 4/11 (K) */
262 #define LEADING_BYTE_CHINESE_CNS11643_5         185
263
264 /* Chinese CNS11643 Set 6       2/4 2/{8,9,10,11} 4/12 (L) */
265 #define LEADING_BYTE_CHINESE_CNS11643_6         186
266
267 /* Chinese CNS11643 Set 7       2/4 2/{8,9,10,11} 4/13 (M) */
268 #define LEADING_BYTE_CHINESE_CNS11643_7         187
269
270 /* DPRK Hangul KPS 9566-1997    2/4 2/{8,9,10,11} 4/14 (N) */
271 #define LEADING_BYTE_KOREAN_KPS9566             202
272
273
274 #define NUM_LEADING_BYTES       (80 * 3 - MIN_LEADING_BYTE)
275
276 \f
277 /************************************************************************/
278 /*            Information about a particular character set              */
279 /************************************************************************/
280
281 struct Lisp_Charset
282 {
283   struct lcrecord_header header;
284
285   int id;
286   Lisp_Object name;
287   Lisp_Object doc_string;
288   Lisp_Object registry;
289   Lisp_Object short_name;
290   Lisp_Object long_name;
291
292   Lisp_Object reverse_direction_charset;
293
294   Lisp_Object ccl_program;
295
296   /* Final byte of this character set in ISO2022 designating escape sequence */
297   Bufbyte final;
298
299   /* Number of columns a character in this charset takes up, on TTY
300      devices.  Not used for X devices. */
301   unsigned int columns;
302
303   /* Direction of this character set */
304   unsigned int direction;
305
306   /* Number of bytes used in encoding of this character set (1 .. 4) */
307   unsigned short dimension;
308
309   /* Number of chars in each dimension (94, 96, 128, 256) */
310   unsigned short chars;
311
312   /* Which half of font to be used to display this character set */
313   unsigned int graphic;
314
315   /* Code-point->character mapping table */
316   Lisp_Object decoding_table;
317
318   /* Character->code-point mapping table */
319   Lisp_Object encoding_table;
320
321   /* Range of character code */
322   Emchar ucs_min, ucs_max;
323
324   /* Offset for external code */
325   Emchar code_offset;
326
327   /* Offset for each byte */
328   Emchar byte_offset;
329 };
330 typedef struct Lisp_Charset Lisp_Charset;
331
332 DECLARE_LRECORD (charset, Lisp_Charset);
333 #define XCHARSET(x) XRECORD (x, charset, Lisp_Charset)
334 #define XSETCHARSET(x, p) XSETRECORD (x, p, charset)
335 #define CHARSETP(x) RECORDP (x, charset)
336 #define GC_CHARSETP(x) GC_RECORDP (x, charset)
337 #define CHECK_CHARSET(x) CHECK_RECORD (x, charset)
338 #define CONCHECK_CHARSET(x) CONCHECK_RECORD (x, charset)
339
340 #define CHARSET_TYPE_94      0 /* This charset includes 94      characters. */
341 #define CHARSET_TYPE_94X94   1 /* This charset includes 94x94   characters. */
342 #define CHARSET_TYPE_96      2 /* This charset includes 96      characters. */
343 #define CHARSET_TYPE_96X96   3 /* This charset includes 96x96   characters. */
344 #define CHARSET_TYPE_128     4 /* This charset includes 128     characters. */
345 #define CHARSET_TYPE_128X128 5 /* This charset includes 128x128 characters. */
346 #define CHARSET_TYPE_256     6 /* This charset includes 256     characters. */
347 #define CHARSET_TYPE_256X256 7 /* This charset includes 256x256 characters. */
348
349 #define CHARSET_LEFT_TO_RIGHT   0
350 #define CHARSET_RIGHT_TO_LEFT   1
351
352 /* Leading byte and id have been regrouped. -- OG */
353 #define CHARSET_ID(cs)           ((cs)->id)
354 #define CHARSET_LEADING_BYTE(cs) (CHARSET_ID(cs))
355 #define CHARSET_NAME(cs)         ((cs)->name)
356 #define CHARSET_SHORT_NAME(cs)   ((cs)->short_name)
357 #define CHARSET_LONG_NAME(cs)    ((cs)->long_name)
358 #define CHARSET_COLUMNS(cs)      ((cs)->columns)
359 #define CHARSET_GRAPHIC(cs)      ((cs)->graphic)
360 #define CHARSET_DIRECTION(cs)    ((cs)->direction)
361 #define CHARSET_FINAL(cs)        ((cs)->final)
362 #define CHARSET_DOC_STRING(cs)   ((cs)->doc_string)
363 #define CHARSET_REGISTRY(cs)     ((cs)->registry)
364 #define CHARSET_CCL_PROGRAM(cs)  ((cs)->ccl_program)
365 #define CHARSET_DIMENSION(cs)    ((cs)->dimension)
366 #define CHARSET_CHARS(cs)        ((cs)->chars)
367 #define CHARSET_REVERSE_DIRECTION_CHARSET(cs) ((cs)->reverse_direction_charset)
368 #define CHARSET_DECODING_TABLE(cs) ((cs)->decoding_table)
369 #define CHARSET_ENCODING_TABLE(cs) ((cs)->encoding_table)
370 #define CHARSET_UCS_MIN(cs)      ((cs)->ucs_min)
371 #define CHARSET_UCS_MAX(cs)      ((cs)->ucs_max)
372 #define CHARSET_CODE_OFFSET(cs)  ((cs)->code_offset)
373 #define CHARSET_BYTE_OFFSET(cs)  ((cs)->byte_offset)
374
375
376 #define XCHARSET_ID(cs)           CHARSET_ID           (XCHARSET (cs))
377 #define XCHARSET_NAME(cs)         CHARSET_NAME         (XCHARSET (cs))
378 #define XCHARSET_SHORT_NAME(cs)   CHARSET_SHORT_NAME   (XCHARSET (cs))
379 #define XCHARSET_LONG_NAME(cs)    CHARSET_LONG_NAME    (XCHARSET (cs))
380 #define XCHARSET_GRAPHIC(cs)      CHARSET_GRAPHIC      (XCHARSET (cs))
381 #define XCHARSET_DIRECTION(cs)    CHARSET_DIRECTION    (XCHARSET (cs))
382 #define XCHARSET_FINAL(cs)        CHARSET_FINAL        (XCHARSET (cs))
383 #define XCHARSET_DOC_STRING(cs)   CHARSET_DOC_STRING   (XCHARSET (cs))
384 #define XCHARSET_REGISTRY(cs)     CHARSET_REGISTRY     (XCHARSET (cs))
385 #define XCHARSET_LEADING_BYTE(cs) CHARSET_LEADING_BYTE (XCHARSET (cs))
386 #define XCHARSET_CCL_PROGRAM(cs)  CHARSET_CCL_PROGRAM  (XCHARSET (cs))
387 #define XCHARSET_DIMENSION(cs)    CHARSET_DIMENSION    (XCHARSET (cs))
388 #define XCHARSET_CHARS(cs)        CHARSET_CHARS        (XCHARSET (cs))
389 #define XCHARSET_REVERSE_DIRECTION_CHARSET(cs) \
390   CHARSET_REVERSE_DIRECTION_CHARSET (XCHARSET (cs))
391 #define XCHARSET_DECODING_TABLE(cs) CHARSET_DECODING_TABLE(XCHARSET(cs))
392 #define XCHARSET_ENCODING_TABLE(cs) CHARSET_ENCODING_TABLE(XCHARSET(cs))
393 #define XCHARSET_UCS_MIN(cs)      CHARSET_UCS_MIN(XCHARSET(cs))
394 #define XCHARSET_UCS_MAX(cs)      CHARSET_UCS_MAX(XCHARSET(cs))
395 #define XCHARSET_CODE_OFFSET(cs)  CHARSET_CODE_OFFSET(XCHARSET(cs))
396 #define XCHARSET_BYTE_OFFSET(cs)  CHARSET_BYTE_OFFSET(XCHARSET(cs))
397
398 struct charset_lookup {
399   /* Table of charsets indexed by (leading byte - 128). */
400   Lisp_Object charset_by_leading_byte[NUM_LEADING_BYTES];
401   
402   /* Table of charsets indexed by type/final-byte/direction. */
403   Lisp_Object charset_by_attributes[4][128];
404
405   Charset_ID next_allocated_leading_byte;
406 };
407
408 extern struct charset_lookup *chlook;
409
410 #ifdef ERROR_CHECK_TYPECHECK
411 /* int not Bufbyte even though that is the actual type of a leading byte.
412    This way, out-ot-range values will get caught rather than automatically
413    truncated. */
414 INLINE_HEADER Lisp_Object CHARSET_BY_LEADING_BYTE (Charset_ID lb);
415 INLINE_HEADER Lisp_Object
416 CHARSET_BY_LEADING_BYTE (Charset_ID lb)
417 {
418   assert (lb >= MIN_LEADING_BYTE &&
419           lb < (MIN_LEADING_BYTE + NUM_LEADING_BYTES));
420   return chlook->charset_by_leading_byte[lb - MIN_LEADING_BYTE];
421 }
422
423 #else
424
425 #define CHARSET_BY_LEADING_BYTE(lb) \
426   (chlook->charset_by_leading_byte[(lb) - MIN_LEADING_BYTE])
427
428 #endif
429
430 #define CHARSET_BY_ATTRIBUTES(type, final, dir) \
431   (chlook->charset_by_attributes[type][final])
432
433 \f
434 /************************************************************************/
435 /*                        Dealing with characters                       */
436 /************************************************************************/
437
438 #define MAX_CHAR_BASIC_LATIN    0x007F
439
440 /*
441 #define MIN_CHAR_GREEK          0x0370
442 #define MAX_CHAR_GREEK          0x03CF
443
444 #define MIN_CHAR_CYRILLIC       0x0400
445 #define MAX_CHAR_CYRILLIC       0x045F
446 */
447
448 #define MIN_CHAR_HEBREW         0x0590
449 #define MAX_CHAR_HEBREW         0x05EF
450
451 #define MIN_CHAR_THAI           0x0E00
452 #define MAX_CHAR_THAI           0x0E5F
453
454 /*
455 #define MIN_CHAR_HIRAGANA       0x3041
456 #define MAX_CHAR_HIRAGANA       0x3093
457
458 #define MIN_CHAR_KATAKANA       0x30A1
459 #define MAX_CHAR_KATAKANA       0x30F6
460 */
461
462 #define MIN_CHAR_HALFWIDTH_KATAKANA     0xFF61
463 #define MAX_CHAR_HALFWIDTH_KATAKANA     0xFF9F
464
465 #define MAX_CHAR_BMP            0x00FFFF
466
467 #define MIN_CHAR_MOJIKYO        0xE00000
468 #define MIN_CHAR_DAIKANWA       MIN_CHAR_MOJIKYO
469 #define MAX_CHAR_DAIKANWA       (MIN_CHAR_MOJIKYO + 50100) /* 0xE0FFFF */
470 /* #define MAX_CHAR_MOJIKYO     0xE9093F */
471 #define MAX_CHAR_MOJIKYO        (MIN_CHAR_MOJIKYO + 94 * 60 * 22)
472
473 #define MIN_CHAR_94             0xE90940
474 #define MAX_CHAR_94             (MIN_CHAR_94 + 94 * 80 - 1)
475 #define MIN_CHAR_96             (MIN_CHAR_94 + 94 * 80)
476 #define MAX_CHAR_96             (MIN_CHAR_96 + 96 * 80 - 1)
477
478 #define MIN_CHAR_94x94          0xE9F6C0
479 #define MIN_CHAR_JIS_X0208_1990 (MIN_CHAR_94x94 + 94 * 94 * 79)
480 #define MAX_CHAR_94x94          (MIN_CHAR_94x94 + 94 * 94 * 80 - 1)
481 #define MAX_CHAR_JIS_X0208_1990 MAX_CHAR_94x94
482 #define MIN_CHAR_96x96          0xF4C000
483 #define MAX_CHAR_96x96          (MIN_CHAR_96x96 + 96 * 96 * 80 - 1)
484
485
486 Emchar make_builtin_char (Lisp_Object charset, int c1, int c2);
487
488 INLINE_HEADER Emchar DECODE_CHAR (Lisp_Object charset, int code_point);
489 INLINE_HEADER Emchar
490 DECODE_CHAR (Lisp_Object charset, int code_point)
491 {
492   int dim = XCHARSET_DIMENSION (charset);
493   Lisp_Object decoding_table = XCHARSET_DECODING_TABLE (charset);
494   int idx;
495   Lisp_Object ch;
496
497   while (dim > 0)
498     {
499       dim--;
500       if ( VECTORP (decoding_table)
501            && ( 0 <= (idx = ((code_point >> (dim * 8))
502                              & 255) - XCHARSET_BYTE_OFFSET (charset)) )
503            && ( idx < XVECTOR_LENGTH (decoding_table) )
504            && !NILP (ch = XVECTOR_DATA(decoding_table)[idx]) )
505         {
506           if (CHARP (ch))
507             return XCHAR (ch);
508           else
509             decoding_table = ch;
510         }
511       else
512         break;
513     }
514   if (XCHARSET_DIMENSION (charset) == 1)
515     return make_builtin_char (charset, code_point, 0);
516   else
517     {
518       int plane = LEADING_BYTE_MOJIKYO_PJ_1 - XCHARSET_ID (charset);
519
520       if ( (0 <= plane) && (plane <= 21) )
521         {
522           int c1 = code_point >> 8;
523           int c2 = code_point & 255;
524
525           if (c1 < 16 + 32)
526             return ' ';
527           else if (c1 < 16 + 32 + 30)
528             return DECODE_CHAR (Vcharset_mojikyo,
529                                 plane * (94 * 60)
530                                 + (c1 - (16 + 32)) * 94
531                                 + (c2 - 33)
532                                 + 1);
533           else if (c1 < 18 + 32 + 30)
534             return ' ';
535           else if (c1 < 18 + 32 + 60)
536             return DECODE_CHAR (Vcharset_mojikyo,
537                                 plane * (94 * 60)
538                                 + (c1 - (18 + 32)) * 94
539                                 + (c2 - 33)
540                                 + 1);
541           else
542             return ' ';
543         }
544       else
545         return make_builtin_char (charset, code_point >> 8, code_point & 255);
546     }
547 }
548
549 /* Return a character whose charset is CHARSET and position-codes
550    are C1 and C2.  TYPE9N character ignores C2. */
551
552 INLINE_HEADER Emchar MAKE_CHAR (Lisp_Object charset, int c1, int c2);
553 INLINE_HEADER Emchar
554 MAKE_CHAR (Lisp_Object charset, int c1, int c2)
555 {
556   if (XCHARSET_DIMENSION (charset) == 1)
557     return DECODE_CHAR (charset, c1);
558   else
559     return DECODE_CHAR (charset, (c1 << 8) | c2);
560 }
561
562 extern Lisp_Object Vcharacter_attribute_table;
563
564 int encode_builtin_char_1 (Emchar c, Lisp_Object* charset);
565 int range_charset_code_point (Lisp_Object charset, Emchar ch);
566
567 INLINE_HEADER int charset_code_point (Lisp_Object charset, Emchar ch);
568 INLINE_HEADER int
569 charset_code_point (Lisp_Object charset, Emchar ch)
570 {
571   Lisp_Object encoding_table = XCHARSET_ENCODING_TABLE (charset);
572   Lisp_Object ret;
573
574   if ( CHAR_ID_TABLE_P (encoding_table)
575        && INTP (ret = get_char_id_table (ch, encoding_table)) )
576     return XINT (ret);
577   else
578     return range_charset_code_point (charset, ch);
579 }
580
581 extern Lisp_Object Vdefault_coded_charset_priority_list;
582 EXFUN (Ffind_charset, 1);
583
584 INLINE_HEADER int encode_char_1 (Emchar ch, Lisp_Object* charset);
585 INLINE_HEADER int
586 encode_char_1 (Emchar ch, Lisp_Object* charset)
587 {
588   Lisp_Object charsets = Vdefault_coded_charset_priority_list;
589
590   while (!NILP (charsets))
591     {
592       *charset = Ffind_charset (Fcar (charsets));
593       if (!NILP (*charset))
594         {
595           Lisp_Object encoding_table = XCHARSET_ENCODING_TABLE (*charset);
596           Lisp_Object ret;
597
598           if ( CHAR_ID_TABLE_P (encoding_table)
599                && INTP (ret = get_char_id_table (ch, encoding_table)) )
600             return XINT (ret);
601           else
602             {
603               int code_point;
604
605               if ((code_point
606                    = range_charset_code_point (*charset, ch)) >= 0)
607                 return code_point;
608             }
609         }
610       charsets = Fcdr (charsets);             
611     }
612   
613   /* otherwise --- maybe for bootstrap */
614   return encode_builtin_char_1 (ch, charset);
615 }
616
617 #define ENCODE_CHAR(ch, charset)        encode_char_1 (ch, &(charset))
618
619 INLINE_HEADER void
620 breakup_char_1 (Emchar c, Lisp_Object *charset, int *c1, int *c2);
621 INLINE_HEADER void
622 breakup_char_1 (Emchar c, Lisp_Object *charset, int *c1, int *c2)
623 {
624   int code_point = encode_char_1 (c, charset);
625
626   if (code_point >= 0)
627     {
628       int dim = XCHARSET_DIMENSION (*charset);
629
630        if (dim == 1)
631          {
632            *c1 = code_point;
633            *c2 = 0;
634          }
635        else /* if (dim == 2) */
636          {
637            *c1 = code_point >> 8;
638            *c2 = code_point & 255;
639          }
640     }
641   else{
642     *c1 = *c2 = 0;
643   }
644 }
645
646 #define BREAKUP_CHAR(ch, charset, b1, b2) \
647   breakup_char_1 (ch, &(charset), &(b1), &(b2))
648
649 INLINE_HEADER Lisp_Object CHAR_CHARSET (Emchar ch);
650 INLINE_HEADER Lisp_Object
651 CHAR_CHARSET (Emchar ch)
652 {
653   Lisp_Object charset;
654  
655   ENCODE_CHAR (ch, charset);
656   return charset;
657 }
658
659 #define CHAR_CHARSET_ID(c)  (XCHARSET_ID(CHAR_CHARSET(c)))
660 #define CHAR_COLUMNS(c)     (CHARSET_COLUMNS(XCHARSET(CHAR_CHARSET(c))))
661
662
663 typedef struct Charc
664 {
665   Lisp_Object charset;
666   int code_point;
667 } Charc;
668
669 INLINE_HEADER Charc CHAR_TO_CHARC (Emchar ch);
670 INLINE_HEADER Charc
671 CHAR_TO_CHARC (Emchar ch)
672 {
673   Charc cc;
674
675   cc.code_point = encode_char_1 (ch, &cc.charset);
676   if (EQ (cc.charset, Vcharset_mojikyo))
677     {
678       int plane, byte1, byte2;
679
680       cc.code_point--;
681       plane = cc.code_point / (94 * 60);
682       byte1 = (cc.code_point % (94 * 60)) / 94;
683       if (byte1 < 30)
684         byte1 += 16 + 32;
685       else
686         byte1 += 18 + 32;
687       byte2 = cc.code_point % 94 + 33;
688       cc.charset
689         = CHARSET_BY_LEADING_BYTE (LEADING_BYTE_MOJIKYO_PJ_1 - plane);
690       cc.code_point = (byte1 << 8) | byte2;
691     }
692   return cc;
693 }
694
695 \f
696 /************************************************************************/
697 /*                            Exported functions                        */
698 /************************************************************************/
699
700 EXFUN (Fget_charset, 1);
701
702 extern Lisp_Object Vcharset_chinese_big5_1;
703 extern Lisp_Object Vcharset_chinese_big5_2;
704 extern Lisp_Object Vcharset_japanese_jisx0208;
705 extern Lisp_Object Vcharset_japanese_jisx0212;
706
707 #endif /* INCLUDED_char_ucs_h_ */