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