Sync up with r21-4-11-chise-0_20-=ucs.
[chise/xemacs-chise.git] / src / char-ucs.h
1 /* Header for UCS-4 character representation.
2    Copyright (C) 1999,2000,2001,2002,2003 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 #include "elhash.h"
26
27 extern Lisp_Object Vchar_attribute_hash_table;
28
29 #define valid_char_p(ch) 1
30
31 #define CHAR_ASCII_P(ch) ((ch) <= 0x7F)
32
33 \f
34 /************************************************************************/
35 /*                            Exported functions                        */
36 /************************************************************************/
37
38 extern Lisp_Object Vcharset_ucs;
39 extern Lisp_Object Vcharset_latin_jisx0201;
40 extern Lisp_Object Vcharset_chinese_big5;
41 extern Lisp_Object Vcharset_chinese_big5_1;
42 extern Lisp_Object Vcharset_chinese_big5_2;
43 extern Lisp_Object Vcharset_japanese_jisx0208;
44 extern Lisp_Object Vcharset_japanese_jisx0208_1990;
45 extern Lisp_Object Vcharset_japanese_jisx0212;
46
47 EXFUN (Fget_charset, 1);
48
49 extern Lisp_Object Qmap_ucs, Qucs;
50
51 Lisp_Object put_char_ccs_code_point (Lisp_Object character,
52                                      Lisp_Object ccs, Lisp_Object value);
53 Lisp_Object remove_char_ccs (Lisp_Object character, Lisp_Object ccs);
54
55 \f
56 /************************************************************************/
57 /*                    Definition of leading bytes                       */
58 /************************************************************************/
59
60 typedef short Charset_ID;
61
62 #define MIN_LEADING_BYTE                -0x200
63
64 /* ISO/IEC 10646 */
65 #define LEADING_BYTE_UCS                (MIN_LEADING_BYTE + 1)
66
67 /* represent normal 80-9F */
68 #define LEADING_BYTE_CONTROL_1          (MIN_LEADING_BYTE + 2)
69
70 /* ISO/IEC 10646 BMP */
71 #define LEADING_BYTE_UCS_BMP            (MIN_LEADING_BYTE + 3)
72
73 /* ISO/IEC 10646 SMP */
74 #define LEADING_BYTE_UCS_SMP            (MIN_LEADING_BYTE + 4)
75
76 /* ISO/IEC 10646 SIP */
77 #define LEADING_BYTE_UCS_SIP            (MIN_LEADING_BYTE + 5)
78
79 /* Japanese JIS X0208 Common    2/4 2/{(8),9,10,11} 4/2 (B) */
80 #define LEADING_BYTE_JIS_X0208          (MIN_LEADING_BYTE + 11)
81
82 /* Japanese JIS X0208-1990      2/4 2/{(8),9,10,11} 4/2 (B) */
83 #define LEADING_BYTE_JAPANESE_JISX0208_1990 (MIN_LEADING_BYTE + 12)
84
85 /* Chinese GB 12345-1990 */
86 #define LEADING_BYTE_CHINESE_GB12345    (MIN_LEADING_BYTE + 13)
87
88 #define LEADING_BYTE_CHINESE_BIG5       (MIN_LEADING_BYTE + 20)
89
90 /* Big5 Level 1                 2/4 2/{(8),9,10,11} 4/0 '0' */
91 #define LEADING_BYTE_CHINESE_BIG5_1     (MIN_LEADING_BYTE + 21)
92
93 /* Big5 Level 2                 2/4 2/{(8),9,10,11} 4/0 '1' */
94 #define LEADING_BYTE_CHINESE_BIG5_2     (MIN_LEADING_BYTE + 22)
95
96 /* VISCII 1.1 */
97 #define LEADING_BYTE_LATIN_VISCII       (MIN_LEADING_BYTE + 24)
98
99 /* MULE VISCII-LOWER                    (CHARSET_ID_OFFSET_96 + '1') */
100 #define LEADING_BYTE_LATIN_VISCII_LOWER (MIN_LEADING_BYTE + 25)
101
102 /* MULE VISCII-UPPER                    (CHARSET_ID_OFFSET_96 + '2') */
103 #define LEADING_BYTE_LATIN_VISCII_UPPER (MIN_LEADING_BYTE + 26)
104
105 #define LEADING_BYTE_ETHIOPIC_UCS       (MIN_LEADING_BYTE + 27)
106
107 #define MIN_LEADING_BYTE_PRIVATE        (MIN_LEADING_BYTE + 97)
108 #define MAX_LEADING_BYTE_PRIVATE        -1
109
110
111 /* ISO 646 IRV */
112 #define LEADING_BYTE_ASCII                6 /* (CHARSET_ID_OFFSET_94 + 'B') */
113
114 /* Right half of JIS X0201-1976 */
115 #define LEADING_BYTE_KATAKANA_JISX0201   13 /* (CHARSET_ID_OFFSET_94 + 'I') */
116
117 /* Left  half of JIS X0201-1976 */
118 #define LEADING_BYTE_LATIN_JISX0201      14 /* (CHARSET_ID_OFFSET_94 + 'J') */
119
120
121 /* #define CHARSET_ID_OFFSET_96         (CHARSET_ID_OFFSET_94 + 80) */
122
123 /* Right half of ISO 8859-1 */
124 #define LEADING_BYTE_LATIN_ISO8859_1    100 /* (CHARSET_ID_OFFSET_96 + 'A') */
125
126 /* Right half of ISO 8859-2 */
127 #define LEADING_BYTE_LATIN_ISO8859_2    101 /* (CHARSET_ID_OFFSET_96 + 'B') */
128
129 /* Right half of ISO 8859-3 */
130 #define LEADING_BYTE_LATIN_ISO8859_3    109 /* (CHARSET_ID_OFFSET_96 + 'C') */
131
132 /* Right half of ISO 8859-4 */
133 #define LEADING_BYTE_LATIN_ISO8859_4    110 /* (CHARSET_ID_OFFSET_96 + 'D') */
134
135 /* Right half of ISO 8859-7 */
136 #define LEADING_BYTE_GREEK_ISO8859_7    126 /* (CHARSET_ID_OFFSET_96 + 'F') */
137
138 /* Right half of ISO 8859-6 */
139 #define LEADING_BYTE_ARABIC_ISO8859_6   127 /* (CHARSET_ID_OFFSET_96 + 'G') */
140
141 /* Right half of ISO 8859-8 */
142 #define LEADING_BYTE_HEBREW_ISO8859_8   138 /* (CHARSET_ID_OFFSET_96 + 'H') */
143
144 /* Right half of ISO 8859-5 */
145 #define LEADING_BYTE_CYRILLIC_ISO8859_5 144 /* (CHARSET_ID_OFFSET_96 + 'L') */
146
147 /* Right half of ISO 8859-9 */
148 #define LEADING_BYTE_LATIN_ISO8859_9    148 /* (CHARSET_ID_OFFSET_96 + 'M') */
149
150 /* TIS620-2533 */
151 #define LEADING_BYTE_THAI_TIS620        166 /* (CHARSET_ID_OFFSET_96 + 'T') */
152
153 /* Right-hand Part of the VSCII-2 (TCVN 5712:1983) */
154 #define LEADING_BYTE_LATIN_TCVN5712     180 /* F = 5/10 0x5A `Z' */
155
156
157 /* #define CHARSET_ID_OFFSET_94x94              (CHARSET_ID_OFFSET_96 + 80) */
158
159 /* #define MIN_LEADING_BYTE_PRIVATE_2   ('0' + CHARSET_ID_OFFSET_94x94) */
160 /* #define MAX_LEADING_BYTE_PRIVATE_2   ('?' + CHARSET_ID_OFFSET_94x94) */
161
162 /* Japanese JIS X0208-1978      2/4 2/{(8),9,10,11} 4/0 (@) */
163 #define LEADING_BYTE_JAPANESE_JISX0208_1978      42
164
165 /* Chinese Hanzi GB2312-1980    2/4 2/{(8),9,10,11} 4/1 (A) */
166 #define LEADING_BYTE_CHINESE_GB2312              58
167
168 /* Japanese JIS X0208-1983      2/4 2/{(8),9,10,11} 4/2 (B) */
169 #define LEADING_BYTE_JAPANESE_JISX0208           87
170
171 /* Hangul KS C5601-1987         2/4 2/{8,9,10,11} 4/3 (C) */
172 #define LEADING_BYTE_KOREAN_KSC5601             149
173
174 /* Japanese JIS X0212-1990      2/4 2/{8,9,10,11} 4/4 (D) */
175 #define LEADING_BYTE_JAPANESE_JISX0212          159
176
177 /* CCITT Extended GB            2/4 2/{8,9,10,11} 4/5 (E) */
178 #define LEADING_BYTE_CHINESE_CCITT_GB           165
179
180 /* Chinese CNS11643 Set 1       2/4 2/{8,9,10,11} 4/7 (G) */
181 #define LEADING_BYTE_CHINESE_CNS11643_1         171
182
183 /* Chinese CNS11643 Set 2       2/4 2/{8,9,10,11} 4/8 (H) */
184 #define LEADING_BYTE_CHINESE_CNS11643_2         172
185
186 /* Chinese CNS11643 Set 3       2/4 2/{8,9,10,11} 4/9 (I) */
187 #define LEADING_BYTE_CHINESE_CNS11643_3         183
188
189 /* Chinese CNS11643 Set 4       2/4 2/{8,9,10,11} 4/10 (J) */
190 #define LEADING_BYTE_CHINESE_CNS11643_4         184
191
192 /* Chinese CNS11643 Set 5       2/4 2/{8,9,10,11} 4/11 (K) */
193 #define LEADING_BYTE_CHINESE_CNS11643_5         185
194
195 /* Chinese CNS11643 Set 6       2/4 2/{8,9,10,11} 4/12 (L) */
196 #define LEADING_BYTE_CHINESE_CNS11643_6         186
197
198 /* Chinese CNS11643 Set 7       2/4 2/{8,9,10,11} 4/13 (M) */
199 #define LEADING_BYTE_CHINESE_CNS11643_7         187
200
201 /* DPRK Hangul KPS 9566-1997    2/4 2/{8,9,10,11} 4/14 (N) */
202 #define LEADING_BYTE_KOREAN_KPS9566             202
203
204
205 #define NUM_LEADING_BYTES       (80 * 3 - MIN_LEADING_BYTE)
206
207 \f
208 /************************************************************************/
209 /*            Information about a particular character set              */
210 /************************************************************************/
211
212 struct Lisp_Charset
213 {
214   struct lcrecord_header header;
215
216   int id;
217   Lisp_Object name;
218   Lisp_Object doc_string;
219   Lisp_Object registry;
220   Lisp_Object short_name;
221   Lisp_Object long_name;
222
223   Lisp_Object reverse_direction_charset;
224
225   Lisp_Object ccl_program;
226
227   /* Final byte of this character set in ISO2022 designating escape sequence */
228   Bufbyte final;
229
230   /* Number of columns a character in this charset takes up, on TTY
231      devices.  Not used for X devices. */
232   unsigned int columns;
233
234   /* Direction of this character set */
235   unsigned int direction;
236
237   /* Number of bytes used in encoding of this character set (1 .. 4) */
238   unsigned short dimension;
239
240   /* Number of chars in each dimension (94, 96, 128, 256) */
241   unsigned short chars;
242
243   /* Which half of font to be used to display this character set */
244   unsigned int graphic;
245
246   /* Code-point->character mapping table */
247   Lisp_Object decoding_table;
248
249   /* Base CCS */
250   Lisp_Object mother;
251
252   /* Range of character code */
253   int min_code, max_code;
254
255   /* Offset for external code */
256   int code_offset;
257
258   /* Type of conversion from mother CCS */
259   unsigned char conversion;
260
261   /* Offset for each byte */
262   int byte_offset;
263 };
264 typedef struct Lisp_Charset Lisp_Charset;
265
266 DECLARE_LRECORD (charset, Lisp_Charset);
267 #define XCHARSET(x) XRECORD (x, charset, Lisp_Charset)
268 #define XSETCHARSET(x, p) XSETRECORD (x, p, charset)
269 #define CHARSETP(x) RECORDP (x, charset)
270 #define CHECK_CHARSET(x) CHECK_RECORD (x, charset)
271 #define CONCHECK_CHARSET(x) CONCHECK_RECORD (x, charset)
272
273 #define CHARSET_LEFT_TO_RIGHT   0
274 #define CHARSET_RIGHT_TO_LEFT   1
275
276 /* Leading byte and id have been regrouped. -- OG */
277 #define CHARSET_ID(cs)           ((cs)->id)
278 #define CHARSET_LEADING_BYTE(cs) (CHARSET_ID(cs))
279 #define CHARSET_NAME(cs)         ((cs)->name)
280 #define CHARSET_SHORT_NAME(cs)   ((cs)->short_name)
281 #define CHARSET_LONG_NAME(cs)    ((cs)->long_name)
282 #define CHARSET_COLUMNS(cs)      ((cs)->columns)
283 #define CHARSET_GRAPHIC(cs)      ((cs)->graphic)
284 #define CHARSET_DIRECTION(cs)    ((cs)->direction)
285 #define CHARSET_FINAL(cs)        ((cs)->final)
286 #define CHARSET_DOC_STRING(cs)   ((cs)->doc_string)
287 #define CHARSET_REGISTRY(cs)     ((cs)->registry)
288 #define CHARSET_CCL_PROGRAM(cs)  ((cs)->ccl_program)
289 #define CHARSET_DIMENSION(cs)    ((cs)->dimension)
290 #define CHARSET_CHARS(cs)        ((cs)->chars)
291 #define CHARSET_REVERSE_DIRECTION_CHARSET(cs) ((cs)->reverse_direction_charset)
292 #define CHARSET_DECODING_TABLE(cs) ((cs)->decoding_table)
293 #define CHARSET_MIN_CODE(cs)     ((cs)->min_code)
294 #define CHARSET_MAX_CODE(cs)     ((cs)->max_code)
295 #define CHARSET_CODE_OFFSET(cs)  ((cs)->code_offset)
296 #define CHARSET_BYTE_OFFSET(cs)  ((cs)->byte_offset)
297 #define CHARSET_MOTHER(cs)       ((cs)->mother)
298 #define CHARSET_CONVERSION(cs)   ((cs)->conversion)
299
300 INLINE_HEADER int CHARSET_BYTE_SIZE (Lisp_Charset* cs);
301 INLINE_HEADER int
302 CHARSET_BYTE_SIZE (Lisp_Charset* cs)
303 {
304   /* ad-hoc method for `ascii' */
305   if ((CHARSET_CHARS (cs) == 94) &&
306       (CHARSET_BYTE_OFFSET (cs) != 33))
307     return 128 - CHARSET_BYTE_OFFSET (cs);
308   else
309     return CHARSET_CHARS (cs);
310 }
311
312 INLINE_HEADER Lisp_Object CHARSET_ENCODING_TABLE (Lisp_Charset* cs);
313 INLINE_HEADER Lisp_Object
314 CHARSET_ENCODING_TABLE (Lisp_Charset* cs)
315 {
316   return Fgethash (CHARSET_NAME(cs),
317                    Vchar_attribute_hash_table,
318                    Qnil);
319 }
320
321 #define CONVERSION_IDENTICAL    0
322 #define CONVERSION_94           1
323 #define CONVERSION_96           2
324 #define CONVERSION_94x60        3
325 #define CONVERSION_94x94        4
326 #define CONVERSION_96x96        5
327 #define CONVERSION_94x94x60     6
328 #define CONVERSION_94x94x94     7
329 #define CONVERSION_96x96x96     8
330 #define CONVERSION_94x94x94x60  9
331 #define CONVERSION_94x94x94x94  10
332 #define CONVERSION_96x96x96x96  11
333
334 #define XCHARSET_ID(cs)           CHARSET_ID           (XCHARSET (cs))
335 #define XCHARSET_NAME(cs)         CHARSET_NAME         (XCHARSET (cs))
336 #define XCHARSET_SHORT_NAME(cs)   CHARSET_SHORT_NAME   (XCHARSET (cs))
337 #define XCHARSET_LONG_NAME(cs)    CHARSET_LONG_NAME    (XCHARSET (cs))
338 #define XCHARSET_GRAPHIC(cs)      CHARSET_GRAPHIC      (XCHARSET (cs))
339 #define XCHARSET_DIRECTION(cs)    CHARSET_DIRECTION    (XCHARSET (cs))
340 #define XCHARSET_FINAL(cs)        CHARSET_FINAL        (XCHARSET (cs))
341 #define XCHARSET_DOC_STRING(cs)   CHARSET_DOC_STRING   (XCHARSET (cs))
342 #define XCHARSET_REGISTRY(cs)     CHARSET_REGISTRY     (XCHARSET (cs))
343 #define XCHARSET_LEADING_BYTE(cs) CHARSET_LEADING_BYTE (XCHARSET (cs))
344 #define XCHARSET_CCL_PROGRAM(cs)  CHARSET_CCL_PROGRAM  (XCHARSET (cs))
345 #define XCHARSET_DIMENSION(cs)    CHARSET_DIMENSION    (XCHARSET (cs))
346 #define XCHARSET_CHARS(cs)        CHARSET_CHARS        (XCHARSET (cs))
347 #define XCHARSET_BYTE_SIZE(cs)    CHARSET_BYTE_SIZE    (XCHARSET (cs))
348 #define XCHARSET_REVERSE_DIRECTION_CHARSET(cs) \
349   CHARSET_REVERSE_DIRECTION_CHARSET (XCHARSET (cs))
350 #define XCHARSET_DECODING_TABLE(cs) CHARSET_DECODING_TABLE(XCHARSET(cs))
351 #define XCHARSET_ENCODING_TABLE(cs) CHARSET_ENCODING_TABLE(XCHARSET(cs))
352 #define XCHARSET_MIN_CODE(cs)     CHARSET_MIN_CODE(XCHARSET(cs))
353 #define XCHARSET_MAX_CODE(cs)     CHARSET_MAX_CODE(XCHARSET(cs))
354 #define XCHARSET_CODE_OFFSET(cs)  CHARSET_CODE_OFFSET(XCHARSET(cs))
355 #define XCHARSET_BYTE_OFFSET(cs)  CHARSET_BYTE_OFFSET(XCHARSET(cs))
356 #define XCHARSET_MOTHER(cs)       CHARSET_MOTHER(XCHARSET(cs))
357 #define XCHARSET_CONVERSION(cs)   CHARSET_CONVERSION(XCHARSET(cs))
358
359 struct charset_lookup {
360   /* Table of charsets indexed by (leading byte - 128). */
361   Lisp_Object charset_by_leading_byte[NUM_LEADING_BYTES];
362   
363   /* Table of charsets indexed by type/final-byte/direction. */
364   Lisp_Object charset_by_attributes[4][128];
365
366   Charset_ID next_allocated_leading_byte;
367 };
368
369 extern struct charset_lookup *chlook;
370
371 #ifdef ERROR_CHECK_TYPECHECK
372 /* int not Bufbyte even though that is the actual type of a leading byte.
373    This way, out-ot-range values will get caught rather than automatically
374    truncated. */
375 INLINE_HEADER Lisp_Object CHARSET_BY_LEADING_BYTE (Charset_ID lb);
376 INLINE_HEADER Lisp_Object
377 CHARSET_BY_LEADING_BYTE (Charset_ID lb)
378 {
379   assert (lb >= MIN_LEADING_BYTE &&
380           lb < (MIN_LEADING_BYTE + NUM_LEADING_BYTES));
381   return chlook->charset_by_leading_byte[lb - MIN_LEADING_BYTE];
382 }
383
384 #else
385
386 #define CHARSET_BY_LEADING_BYTE(lb) \
387   (chlook->charset_by_leading_byte[(lb) - MIN_LEADING_BYTE])
388
389 #endif
390
391 INLINE_HEADER Lisp_Object
392 CHARSET_BY_ATTRIBUTES (int chars, int dimension, int final, int dir);
393 INLINE_HEADER Lisp_Object
394 CHARSET_BY_ATTRIBUTES (int chars, int dimension, int final, int dir)
395 {
396   int type;
397
398   if (chars == 94)
399     type = 0;
400   else if (chars == 96)
401     type = 1;
402   else
403     return Qnil;
404   type += dimension == 1 ? 0 : 2;
405   return chlook->charset_by_attributes[type][final];
406 }
407
408 \f
409 /************************************************************************/
410 /*                        Dealing with characters                       */
411 /************************************************************************/
412
413 #define MAX_CHAR_BASIC_LATIN    0x007F
414
415 /*
416 #define MIN_CHAR_HEBREW         0x0590
417 #define MAX_CHAR_HEBREW         0x05EF
418 */
419
420 #define MIN_CHAR_THAI           0x0E00
421 #define MAX_CHAR_THAI           0x0E5F
422
423 /*
424 #define MIN_CHAR_HIRAGANA       0x3041
425 #define MAX_CHAR_HIRAGANA       0x3093
426
427 #define MIN_CHAR_KATAKANA       0x30A1
428 #define MAX_CHAR_KATAKANA       0x30F6
429 */
430
431 #define MIN_CHAR_HALFWIDTH_KATAKANA     0xFF61
432 #define MAX_CHAR_HALFWIDTH_KATAKANA     0xFF9F
433
434 #define MAX_CHAR_BMP            0x00FFFF
435
436 #define MIN_CHAR_SMP            0x010000
437 #define MAX_CHAR_SMP            0x01FFFF
438
439 #define MIN_CHAR_SIP            0x020000
440 #define MAX_CHAR_SIP            0x02FFFF
441
442 #if 0
443 #define MIN_CHAR_DAIKANWA       0x00E00000
444 #define MAX_CHAR_DAIKANWA       (MIN_CHAR_DAIKANWA + 50100) /* 0xE0FFFF */
445
446 #define MIN_CHAR_CBETA          0x00E20000
447 #define MAX_CHAR_CBETA          0x00E2FFFF
448
449 #define MIN_CHAR_CHINA3_JEF     0x00E80000
450 #define MAX_CHAR_CHINA3_JEF     0x00E8FFFF
451 #endif
452
453 #define MIN_CHAR_94             0x00E90940
454 #define MAX_CHAR_94             (MIN_CHAR_94 + 94 * 80 - 1)
455 #define MIN_CHAR_96             (MIN_CHAR_94 + 94 * 80)
456 #define MAX_CHAR_96             (MIN_CHAR_96 + 96 * 80 - 1)
457
458 #define MIN_CHAR_94x94          0xE9F6C0
459 #define MIN_CHAR_JIS_X0208_1990 (MIN_CHAR_94x94 + 94 * 94 * 79)
460 #define MAX_CHAR_94x94          (MIN_CHAR_94x94 + 94 * 94 * 80 - 1)
461 #define MAX_CHAR_JIS_X0208_1990 MAX_CHAR_94x94
462 #define MIN_CHAR_96x96          0xF4C000
463 #define MAX_CHAR_96x96          (MIN_CHAR_96x96 + 96 * 96 * 80 - 1)
464
465 /*
466 #define MIN_CHAR_GT             0x61000000
467 #define MAX_CHAR_GT             (MIN_CHAR_GT + 67547)
468 */
469 #define MIN_CHAR_BIG5_CDP       0x62000000
470 #define MAX_CHAR_BIG5_CDP       0x6200FFFF
471 /*
472 #define MIN_CHAR_HANZIKU_1      (0x62000000 + 65536 * 1)
473 #define MAX_CHAR_HANZIKU_1      (0x62000000 + 65536 * 1 + 65535)
474 #define MIN_CHAR_HANZIKU_2      (0x62000000 + 65536 * 2)
475 #define MAX_CHAR_HANZIKU_2      (0x62000000 + 65536 * 2 + 65535)
476 #define MIN_CHAR_HANZIKU_3      (0x62000000 + 65536 * 3)
477 #define MAX_CHAR_HANZIKU_3      (0x62000000 + 65536 * 3 + 65535)
478 #define MIN_CHAR_HANZIKU_4      (0x62000000 + 65536 * 4)
479 #define MAX_CHAR_HANZIKU_4      (0x62000000 + 65536 * 4 + 65535)
480 #define MIN_CHAR_HANZIKU_5      (0x62000000 + 65536 * 5)
481 #define MAX_CHAR_HANZIKU_5      (0x62000000 + 65536 * 5 + 65535)
482 #define MIN_CHAR_HANZIKU_6      (0x62000000 + 65536 * 6)
483 #define MAX_CHAR_HANZIKU_6      (0x62000000 + 65536 * 6 + 65535)
484 #define MIN_CHAR_HANZIKU_7      (0x62000000 + 65536 * 7)
485 #define MAX_CHAR_HANZIKU_7      (0x62000000 + 65536 * 7 + 65535)
486 #define MIN_CHAR_HANZIKU_8      (0x62000000 + 65536 * 8)
487 #define MAX_CHAR_HANZIKU_8      (0x62000000 + 65536 * 8 + 65535)
488 #define MIN_CHAR_HANZIKU_9      (0x62000000 + 65536 * 9)
489 #define MAX_CHAR_HANZIKU_9      (0x62000000 + 65536 * 9 + 65535)
490 #define MIN_CHAR_HANZIKU_10     (0x62000000 + 65536 * 10)
491 #define MAX_CHAR_HANZIKU_10     (0x62000000 + 65536 * 10 + 65535)
492 #define MIN_CHAR_HANZIKU_11     (0x62000000 + 65536 * 11)
493 #define MAX_CHAR_HANZIKU_11     (0x62000000 + 65536 * 11 + 65535)
494 #define MIN_CHAR_HANZIKU_12     (0x62000000 + 65536 * 12)
495 #define MAX_CHAR_HANZIKU_12     (0x62000000 + 65536 * 12 + 65535)
496 */
497
498 Emchar decode_builtin_char (Lisp_Object charset, int code_point);
499
500 INLINE_HEADER Lisp_Object
501 get_ccs_octet_table (Lisp_Object table, Lisp_Object ccs, int code);
502 INLINE_HEADER Lisp_Object
503 get_ccs_octet_table (Lisp_Object table, Lisp_Object ccs, int code)
504 {
505   int byte_offset = XCHARSET_BYTE_OFFSET (ccs);
506
507   if (VECTORP (table))
508     {
509       int idx = code - byte_offset;
510
511       if (idx < XVECTOR_LENGTH(table))
512         return XVECTOR_DATA(table)[idx];
513       else
514         return Qunbound;
515     }
516   else
517     return table;
518 }
519
520 INLINE_HEADER Lisp_Object
521 put_ccs_octet_table (Lisp_Object table, Lisp_Object ccs, int code,
522                      Lisp_Object value);
523 INLINE_HEADER Lisp_Object
524 put_ccs_octet_table (Lisp_Object table, Lisp_Object ccs, int code,
525                      Lisp_Object value)
526 {
527   int byte_offset = XCHARSET_BYTE_OFFSET (ccs);
528   int ccs_len = XCHARSET_BYTE_SIZE (ccs);
529
530   if (VECTORP (table))
531     {
532       XVECTOR_DATA(table)[code - byte_offset] = value;
533       return table;
534     }
535   else if (EQ (table, value))
536     return table;
537   else
538     {
539       table = make_vector (ccs_len, table);
540       XVECTOR_DATA(table)[code - byte_offset] = value;
541       return table;
542     }
543 }
544
545 INLINE_HEADER void
546 decoding_table_put_char (Lisp_Object ccs,
547                          int code_point, Lisp_Object character);
548 INLINE_HEADER void
549 decoding_table_put_char (Lisp_Object ccs,
550                          int code_point, Lisp_Object character)
551 {
552   Lisp_Object table1 = XCHARSET_DECODING_TABLE (ccs);
553   int dim = XCHARSET_DIMENSION (ccs);
554
555   if (dim == 1)
556     XCHARSET_DECODING_TABLE (ccs)
557       = put_ccs_octet_table (table1, ccs, code_point, character);
558   else if (dim == 2)
559     {
560       Lisp_Object table2
561         = get_ccs_octet_table (table1, ccs, (unsigned char)(code_point >> 8));
562
563       table2 = put_ccs_octet_table (table2, ccs,
564                                     (unsigned char)code_point, character);
565       XCHARSET_DECODING_TABLE (ccs)
566         = put_ccs_octet_table (table1, ccs,
567                                (unsigned char)(code_point >> 8), table2);
568     }
569   else if (dim == 3)
570     {
571       Lisp_Object table2
572         = get_ccs_octet_table (table1, ccs, (unsigned char)(code_point >> 16));
573       Lisp_Object table3
574         = get_ccs_octet_table (table2, ccs, (unsigned char)(code_point >>  8));
575
576       table3 = put_ccs_octet_table (table3, ccs,
577                                     (unsigned char)code_point, character);
578       table2 = put_ccs_octet_table (table2, ccs,
579                                     (unsigned char)(code_point >> 8), table3);
580       XCHARSET_DECODING_TABLE (ccs)
581         = put_ccs_octet_table (table1, ccs,
582                                (unsigned char)(code_point >> 16), table2);
583     }
584   else /* if (dim == 4) */
585     {
586       Lisp_Object table2
587         = get_ccs_octet_table (table1, ccs, (unsigned char)(code_point >> 24));
588       Lisp_Object table3
589         = get_ccs_octet_table (table2, ccs, (unsigned char)(code_point >> 16));
590       Lisp_Object table4
591         = get_ccs_octet_table (table3, ccs, (unsigned char)(code_point >>  8));
592
593       table4 = put_ccs_octet_table (table4, ccs,
594                                     (unsigned char)code_point, character);
595       table3 = put_ccs_octet_table (table3, ccs,
596                                     (unsigned char)(code_point >>  8), table4);
597       table2 = put_ccs_octet_table (table2, ccs,
598                                     (unsigned char)(code_point >> 16), table3);
599       XCHARSET_DECODING_TABLE (ccs)
600         = put_ccs_octet_table (table1, ccs,
601                                (unsigned char)(code_point >> 24), table2);
602     }
603 }
604
605 INLINE_HEADER void
606 decoding_table_remove_char (Lisp_Object ccs, int code_point);
607 INLINE_HEADER void
608 decoding_table_remove_char (Lisp_Object ccs, int code_point)
609 {
610   decoding_table_put_char (ccs, code_point, Qunbound);
611 }
612
613 Emchar decode_defined_char (Lisp_Object charset, int code_point);
614
615 INLINE_HEADER Emchar DECODE_CHAR (Lisp_Object charset, int code_point);
616 INLINE_HEADER Emchar
617 DECODE_CHAR (Lisp_Object charset, int code_point)
618 {
619   Emchar char_id = decode_defined_char (charset, code_point);
620
621   if (char_id >= 0)
622     return char_id;
623   else if (XCHARSET_DIMENSION (charset) == 1)
624     return decode_builtin_char (charset, code_point);
625   else if (EQ (charset, Vcharset_chinese_big5_1))
626     {
627       unsigned int I
628         = ((code_point >> 8) - 33) * (0xFF - 0xA1)
629         + ((code_point & 0xFF) - 33);
630       unsigned char b1 = I / (0xFF - 0xA1 + 0x7F - 0x40) + 0xA1;
631       unsigned char b2 = I % (0xFF - 0xA1 + 0x7F - 0x40);
632
633       b2 += b2 < 0x3F ? 0x40 : 0x62;
634       return DECODE_CHAR (Vcharset_chinese_big5, (b1 << 8) | b2);
635     }
636   else if (EQ (charset, Vcharset_chinese_big5_2))
637     {
638       unsigned int I
639         = ((code_point >> 8) - 33) * (0xFF - 0xA1)
640         + ((code_point & 0xFF) - 33);
641       unsigned char b1, b2;
642
643       I += (0xFF - 0xA1 + 0x7F - 0x40) * (0xC9 - 0xA1);
644       b1 = I / (0xFF - 0xA1 + 0x7F - 0x40) + 0xA1;
645       b2 = I % (0xFF - 0xA1 + 0x7F - 0x40);
646       b2 += b2 < 0x3F ? 0x40 : 0x62;
647       return DECODE_CHAR (Vcharset_chinese_big5, (b1 << 8) | b2);
648     }
649   return decode_builtin_char (charset, code_point);
650 }
651
652 /* Return a character whose charset is CHARSET and position-codes
653    are C1 and C2.  TYPE9N character ignores C2. */
654
655 INLINE_HEADER Emchar MAKE_CHAR (Lisp_Object charset, int c1, int c2);
656 INLINE_HEADER Emchar
657 MAKE_CHAR (Lisp_Object charset, int c1, int c2)
658 {
659   if (XCHARSET_DIMENSION (charset) == 1)
660     return DECODE_CHAR (charset, c1);
661   else
662     return DECODE_CHAR (charset, (c1 << 8) | c2);
663 }
664
665 extern Lisp_Object Vcharacter_attribute_table;
666
667 int encode_builtin_char_1 (Emchar c, Lisp_Object* charset);
668 int charset_code_point (Lisp_Object charset, Emchar ch, int defined_only);
669 int range_charset_code_point (Lisp_Object charset, Emchar ch);
670
671 extern Lisp_Object Vdefault_coded_charset_priority_list;
672 EXFUN (Ffind_charset, 1);
673
674 INLINE_HEADER int encode_char_1 (Emchar ch, Lisp_Object* charset);
675 INLINE_HEADER int
676 encode_char_1 (Emchar ch, Lisp_Object* charset)
677 {
678   Lisp_Object charsets = Vdefault_coded_charset_priority_list;
679
680   while (!NILP (charsets))
681     {
682       *charset = Ffind_charset (Fcar (charsets));
683       if (!NILP (*charset))
684         {
685           int code_point = charset_code_point (*charset, ch, 0);
686
687           if (code_point >= 0)
688             return code_point;
689         }
690       charsets = Fcdr (charsets);             
691     }
692   
693   /* otherwise --- maybe for bootstrap */
694   return encode_builtin_char_1 (ch, charset);
695 }
696
697 #define ENCODE_CHAR(ch, charset)        encode_char_1 (ch, &(charset))
698
699 INLINE_HEADER void
700 breakup_char_1 (Emchar c, Lisp_Object *charset, int *c1, int *c2);
701 INLINE_HEADER void
702 breakup_char_1 (Emchar c, Lisp_Object *charset, int *c1, int *c2)
703 {
704   int code_point = encode_char_1 (c, charset);
705
706   if (code_point >= 0)
707     {
708       int dim = XCHARSET_DIMENSION (*charset);
709
710        if (dim == 1)
711          {
712            *c1 = code_point;
713            *c2 = 0;
714          }
715        else /* if (dim == 2) */
716          {
717            *c1 = code_point >> 8;
718            *c2 = code_point & 255;
719          }
720     }
721   else{
722     *c1 = *c2 = 0;
723   }
724 }
725
726 #define BREAKUP_CHAR(ch, charset, b1, b2) \
727   breakup_char_1 (ch, &(charset), &(b1), &(b2))
728
729 INLINE_HEADER Lisp_Object CHAR_CHARSET (Emchar ch);
730 INLINE_HEADER Lisp_Object
731 CHAR_CHARSET (Emchar ch)
732 {
733   Lisp_Object charset;
734  
735   ENCODE_CHAR (ch, charset);
736   return charset;
737 }
738
739 #define CHAR_CHARSET_ID(c)  (XCHARSET_ID(CHAR_CHARSET(c)))
740 #define CHAR_COLUMNS(c)     (CHARSET_COLUMNS(XCHARSET(CHAR_CHARSET(c))))
741
742
743 typedef struct Charc
744 {
745   Lisp_Object charset;
746   int code_point;
747 } Charc;
748
749 INLINE_HEADER Charc CHAR_TO_CHARC (Emchar ch);
750 INLINE_HEADER Charc
751 CHAR_TO_CHARC (Emchar ch)
752 {
753   Charc cc;
754
755   cc.code_point = encode_char_1 (ch, &cc.charset);
756   return cc;
757 }
758
759 #endif /* INCLUDED_char_ucs_h_ */