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