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