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