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