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