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