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