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