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