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