(Qucs): New external variable.
[chise/xemacs-chise.git] / src / char-ucs.h
1 /* Header for UCS-4 character representation.
2    Copyright (C) 1999,2000,2001 MORIOKA Tomohiko
3
4 This file is part of XEmacs.
5
6 XEmacs is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
10
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with XEmacs; see the file COPYING.  If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21 #ifndef INCLUDED_char_ucs_h_
22 #define INCLUDED_char_ucs_h_
23
24 #define valid_char_p(ch) 1
25
26 #define CHAR_ASCII_P(ch) ((ch) <= 0x7F)
27
28 \f
29 /************************************************************************/
30 /*                               Char-ID Tables                         */
31 /************************************************************************/
32
33 struct Lisp_Uint8_Byte_Table
34 {
35   struct lcrecord_header header;
36
37   unsigned char property[256];
38 };
39 typedef struct Lisp_Uint8_Byte_Table Lisp_Uint8_Byte_Table;
40
41 DECLARE_LRECORD (uint8_byte_table, Lisp_Uint8_Byte_Table);
42 #define XUINT8_BYTE_TABLE(x) \
43    XRECORD (x, uint8_byte_table, Lisp_Uint8_Byte_Table)
44 #define XSETUINT8_BYTE_TABLE(x, p) XSETRECORD (x, p, uint8_byte_table)
45 #define UINT8_BYTE_TABLE_P(x) RECORDP (x, uint8_byte_table)
46 #define GC_UINT8_BYTE_TABLE_P(x) GC_RECORDP (x, uint8_byte_table)
47 /* #define CHECK_UINT8_BYTE_TABLE(x) CHECK_RECORD (x, uint8_byte_table)
48    char table entries should never escape to Lisp */
49
50
51 struct Lisp_Uint16_Byte_Table
52 {
53   struct lcrecord_header header;
54
55   unsigned short property[256];
56 };
57 typedef struct Lisp_Uint16_Byte_Table Lisp_Uint16_Byte_Table;
58
59 DECLARE_LRECORD (uint16_byte_table, Lisp_Uint16_Byte_Table);
60 #define XUINT16_BYTE_TABLE(x) \
61    XRECORD (x, uint16_byte_table, Lisp_Uint16_Byte_Table)
62 #define XSETUINT16_BYTE_TABLE(x, p) XSETRECORD (x, p, uint16_byte_table)
63 #define UINT16_BYTE_TABLE_P(x) RECORDP (x, uint16_byte_table)
64 #define GC_UINT16_BYTE_TABLE_P(x) GC_RECORDP (x, uint16_byte_table)
65 /* #define CHECK_UINT16_BYTE_TABLE(x) CHECK_RECORD (x, uint16_byte_table)
66    char table entries should never escape to Lisp */
67
68
69 struct Lisp_Byte_Table
70 {
71   struct lcrecord_header header;
72
73   Lisp_Object property[256];
74 };
75 typedef struct Lisp_Byte_Table Lisp_Byte_Table;
76
77 DECLARE_LRECORD (byte_table, Lisp_Byte_Table);
78 #define XBYTE_TABLE(x) XRECORD (x, byte_table, Lisp_Byte_Table)
79 #define XSETBYTE_TABLE(x, p) XSETRECORD (x, p, byte_table)
80 #define BYTE_TABLE_P(x) RECORDP (x, byte_table)
81 #define GC_BYTE_TABLE_P(x) GC_RECORDP (x, byte_table)
82 /* #define CHECK_BYTE_TABLE(x) CHECK_RECORD (x, byte_table)
83    char table entries should never escape to Lisp */
84
85
86 struct Lisp_Char_ID_Table
87 {
88   struct lcrecord_header header;
89
90   Lisp_Object table;
91 };
92 typedef struct Lisp_Char_ID_Table Lisp_Char_ID_Table;
93
94 DECLARE_LRECORD (char_id_table, Lisp_Char_ID_Table);
95 #define XCHAR_ID_TABLE(x) XRECORD (x, char_id_table, Lisp_Char_ID_Table)
96 #define XSETCHAR_ID_TABLE(x, p) XSETRECORD (x, p, char_id_table)
97 #define CHAR_ID_TABLE_P(x) RECORDP (x, char_id_table)
98 #define GC_CHAR_ID_TABLE_P(x) GC_RECORDP (x, char_id_table)
99 /* #define CHECK_CHAR_ID_TABLE(x) CHECK_RECORD (x, char_id_table)
100    char table entries should never escape to Lisp */
101
102
103 Lisp_Object get_char_id_table (Emchar ch, Lisp_Object table);
104
105
106 extern Lisp_Object Vcharset_mojikyo;
107 extern Lisp_Object Vcharset_mojikyo_2022_1;
108 extern Lisp_Object Vcharset_latin_jisx0201;
109
110 \f
111 /************************************************************************/
112 /*                    Definition of leading bytes                       */
113 /************************************************************************/
114
115 typedef short Charset_ID;
116
117 #define MIN_LEADING_BYTE                -0x70
118 #define CHARSET_ID_OFFSET               0x00
119
120 /* ISO/IEC 10646 */
121 #define LEADING_BYTE_UCS                (CHARSET_ID_OFFSET - 1)
122
123 /* represent normal 80-9F */
124 #define LEADING_BYTE_CONTROL_1          (CHARSET_ID_OFFSET - 2)
125
126 /* ISO/IEC 10646 BMP */
127 #define LEADING_BYTE_UCS_BMP            (CHARSET_ID_OFFSET - 3)
128
129 /* ISO/IEC 10646 for CNS */
130 #define LEADING_BYTE_UCS_CNS            (CHARSET_ID_OFFSET - 5)
131
132 /* ISO/IEC 10646 for JIS */
133 #define LEADING_BYTE_UCS_JIS            (CHARSET_ID_OFFSET - 6)
134
135 /* ISO/IEC 10646 for Big5 */
136 #define LEADING_BYTE_UCS_BIG5           (CHARSET_ID_OFFSET - 9)
137
138 /* Japanese JIS X0208-1990      2/4 2/{(8),9,10,11} 4/2 (B) */
139 #define LEADING_BYTE_JAPANESE_JISX0208_1990 (CHARSET_ID_OFFSET - 10)
140
141 /* Chinese GB 12345-1990 */
142 #define LEADING_BYTE_CHINESE_GB12345    (CHARSET_ID_OFFSET - 11)
143
144 #define LEADING_BYTE_CHINESE_BIG5       (CHARSET_ID_OFFSET - 20)
145
146 /* Big5 Level 1                 2/4 2/{(8),9,10,11} 4/0 '0' */
147 #define LEADING_BYTE_CHINESE_BIG5_1     (CHARSET_ID_OFFSET - 21)
148
149 /* Big5 Level 2                 2/4 2/{(8),9,10,11} 4/0 '1' */
150 #define LEADING_BYTE_CHINESE_BIG5_2     (CHARSET_ID_OFFSET - 22)
151
152 #define LEADING_BYTE_CHINESE_BIG5_CDP   (CHARSET_ID_OFFSET - 23)
153
154 /* VISCII 1.1 */
155 #define LEADING_BYTE_LATIN_VISCII       (CHARSET_ID_OFFSET - 24)
156
157 /* MULE VISCII-LOWER                    (CHARSET_ID_OFFSET_96 + '1') */
158 #define LEADING_BYTE_LATIN_VISCII_LOWER (CHARSET_ID_OFFSET - 25)
159
160 /* MULE VISCII-UPPER                    (CHARSET_ID_OFFSET_96 + '2') */
161 #define LEADING_BYTE_LATIN_VISCII_UPPER (CHARSET_ID_OFFSET - 26)
162
163 #define LEADING_BYTE_ETHIOPIC_UCS       (CHARSET_ID_OFFSET - 27)
164
165 #define LEADING_BYTE_DAIKANWA           (CHARSET_ID_OFFSET - 28)
166 #define LEADING_BYTE_DAIKANWA_EKANJI    (CHARSET_ID_OFFSET - 29)
167
168 #define LEADING_BYTE_GT                 (CHARSET_ID_OFFSET - 30)
169 #define LEADING_BYTE_GT_PJ_1            (CHARSET_ID_OFFSET - 31)
170 #define LEADING_BYTE_GT_PJ_2            (CHARSET_ID_OFFSET - 32)
171 #define LEADING_BYTE_GT_PJ_3            (CHARSET_ID_OFFSET - 33)
172 #define LEADING_BYTE_GT_PJ_4            (CHARSET_ID_OFFSET - 34)
173 #define LEADING_BYTE_GT_PJ_5            (CHARSET_ID_OFFSET - 35)
174 #define LEADING_BYTE_GT_PJ_6            (CHARSET_ID_OFFSET - 36)
175 #define LEADING_BYTE_GT_PJ_7            (CHARSET_ID_OFFSET - 37)
176 #define LEADING_BYTE_GT_PJ_8            (CHARSET_ID_OFFSET - 38)
177 #define LEADING_BYTE_GT_PJ_9            (CHARSET_ID_OFFSET - 39)
178 #define LEADING_BYTE_GT_PJ_10           (CHARSET_ID_OFFSET - 40)
179 #define LEADING_BYTE_GT_PJ_11           (CHARSET_ID_OFFSET - 41)
180
181 #define LEADING_BYTE_MOJIKYO            (CHARSET_ID_OFFSET - 48)
182 #define LEADING_BYTE_MOJIKYO_2022_1     (CHARSET_ID_OFFSET - 49)
183 #define LEADING_BYTE_MOJIKYO_2022_2     (CHARSET_ID_OFFSET - 50)
184
185 /* Konjaku-Mojikyo font (for pseudo-JIS X 0208 encoding) */
186 #define LEADING_BYTE_MOJIKYO_PJ_1       (CHARSET_ID_OFFSET - 51)
187 #define LEADING_BYTE_MOJIKYO_PJ_2       (CHARSET_ID_OFFSET - 52)
188 #define LEADING_BYTE_MOJIKYO_PJ_3       (CHARSET_ID_OFFSET - 53)
189 #define LEADING_BYTE_MOJIKYO_PJ_4       (CHARSET_ID_OFFSET - 54)
190 #define LEADING_BYTE_MOJIKYO_PJ_5       (CHARSET_ID_OFFSET - 55)
191 #define LEADING_BYTE_MOJIKYO_PJ_6       (CHARSET_ID_OFFSET - 56)
192 #define LEADING_BYTE_MOJIKYO_PJ_7       (CHARSET_ID_OFFSET - 57)
193 #define LEADING_BYTE_MOJIKYO_PJ_8       (CHARSET_ID_OFFSET - 58)
194 #define LEADING_BYTE_MOJIKYO_PJ_9       (CHARSET_ID_OFFSET - 59)
195 #define LEADING_BYTE_MOJIKYO_PJ_10      (CHARSET_ID_OFFSET - 60)
196 #define LEADING_BYTE_MOJIKYO_PJ_11      (CHARSET_ID_OFFSET - 61)
197 #define LEADING_BYTE_MOJIKYO_PJ_12      (CHARSET_ID_OFFSET - 62)
198 #define LEADING_BYTE_MOJIKYO_PJ_13      (CHARSET_ID_OFFSET - 63)
199 #define LEADING_BYTE_MOJIKYO_PJ_14      (CHARSET_ID_OFFSET - 64)
200 #define LEADING_BYTE_MOJIKYO_PJ_15      (CHARSET_ID_OFFSET - 65)
201 #define LEADING_BYTE_MOJIKYO_PJ_16      (CHARSET_ID_OFFSET - 66)
202 #define LEADING_BYTE_MOJIKYO_PJ_17      (CHARSET_ID_OFFSET - 67)
203 #define LEADING_BYTE_MOJIKYO_PJ_18      (CHARSET_ID_OFFSET - 68)
204 #define LEADING_BYTE_MOJIKYO_PJ_19      (CHARSET_ID_OFFSET - 69)
205 #define LEADING_BYTE_MOJIKYO_PJ_20      (CHARSET_ID_OFFSET - 70)
206 #define LEADING_BYTE_MOJIKYO_PJ_21      (CHARSET_ID_OFFSET - 71)
207
208 #define MIN_LEADING_BYTE_PRIVATE        MIN_LEADING_BYTE
209 #define MAX_LEADING_BYTE_PRIVATE        (CHARSET_ID_OFFSET - 72)
210
211
212 /* #define CHARSET_ID_OFFSET_94         (CHARSET_ID_OFFSET - '0') */
213
214 /* #define MIN_CHARSET_ID_PRIVATE_94    (CHARSET_ID_OFFSET_94 + '0') */
215 /* #define MAX_CHARSET_ID_PRIVATE_94    (CHARSET_ID_OFFSET_94 + '?') */
216
217 /* ISO 646 IRV */
218 #define LEADING_BYTE_ASCII                6 /* (CHARSET_ID_OFFSET_94 + 'B') */
219
220 /* Right half of JIS X0201-1976 */
221 #define LEADING_BYTE_KATAKANA_JISX0201   13 /* (CHARSET_ID_OFFSET_94 + 'I') */
222
223 /* Left  half of JIS X0201-1976 */
224 #define LEADING_BYTE_LATIN_JISX0201      14 /* (CHARSET_ID_OFFSET_94 + 'J') */
225
226
227 /* #define CHARSET_ID_OFFSET_96         (CHARSET_ID_OFFSET_94 + 80) */
228
229 /* Right half of ISO 8859-1 */
230 #define LEADING_BYTE_LATIN_ISO8859_1    100 /* (CHARSET_ID_OFFSET_96 + 'A') */
231
232 /* Right half of ISO 8859-2 */
233 #define LEADING_BYTE_LATIN_ISO8859_2    101 /* (CHARSET_ID_OFFSET_96 + 'B') */
234
235 /* Right half of ISO 8859-3 */
236 #define LEADING_BYTE_LATIN_ISO8859_3    109 /* (CHARSET_ID_OFFSET_96 + 'C') */
237
238 /* Right half of ISO 8859-4 */
239 #define LEADING_BYTE_LATIN_ISO8859_4    110 /* (CHARSET_ID_OFFSET_96 + 'D') */
240
241 /* Right half of ISO 8859-7 */
242 #define LEADING_BYTE_GREEK_ISO8859_7    126 /* (CHARSET_ID_OFFSET_96 + 'F') */
243
244 /* Right half of ISO 8859-6 */
245 #define LEADING_BYTE_ARABIC_ISO8859_6   127 /* (CHARSET_ID_OFFSET_96 + 'G') */
246
247 /* Right half of ISO 8859-8 */
248 #define LEADING_BYTE_HEBREW_ISO8859_8   138 /* (CHARSET_ID_OFFSET_96 + 'H') */
249
250 /* Right half of ISO 8859-5 */
251 #define LEADING_BYTE_CYRILLIC_ISO8859_5 144 /* (CHARSET_ID_OFFSET_96 + 'L') */
252
253 /* Right half of ISO 8859-9 */
254 #define LEADING_BYTE_LATIN_ISO8859_9    148 /* (CHARSET_ID_OFFSET_96 + 'M') */
255
256 /* TIS620-2533 */
257 #define LEADING_BYTE_THAI_TIS620        166 /* (CHARSET_ID_OFFSET_96 + 'T') */
258
259 /* Right-hand Part of the VSCII-2 (TCVN 5712:1983) */
260 #define LEADING_BYTE_LATIN_TCVN5712     180 /* F = 5/10 0x5A `Z' */
261
262
263 /* #define CHARSET_ID_OFFSET_94x94              (CHARSET_ID_OFFSET_96 + 80) */
264
265 /* #define MIN_LEADING_BYTE_PRIVATE_2   ('0' + CHARSET_ID_OFFSET_94x94) */
266 /* #define MAX_LEADING_BYTE_PRIVATE_2   ('?' + CHARSET_ID_OFFSET_94x94) */
267
268 /* Japanese JIS X0208-1978      2/4 2/{(8),9,10,11} 4/0 (@) */
269 #define LEADING_BYTE_JAPANESE_JISX0208_1978      42
270
271 /* Chinese Hanzi GB2312-1980    2/4 2/{(8),9,10,11} 4/1 (A) */
272 #define LEADING_BYTE_CHINESE_GB2312              58
273
274 /* Japanese JIS X0208-1983      2/4 2/{(8),9,10,11} 4/2 (B) */
275 #define LEADING_BYTE_JAPANESE_JISX0208           87
276
277 /* Hangul KS C5601-1987         2/4 2/{8,9,10,11} 4/3 (C) */
278 #define LEADING_BYTE_KOREAN_KSC5601             149
279
280 /* Japanese JIS X0212-1990      2/4 2/{8,9,10,11} 4/4 (D) */
281 #define LEADING_BYTE_JAPANESE_JISX0212          159
282
283 /* CCITT Extended GB            2/4 2/{8,9,10,11} 4/5 (E) */
284 #define LEADING_BYTE_CHINESE_CCITT_GB           165
285
286 /* Chinese CNS11643 Set 1       2/4 2/{8,9,10,11} 4/7 (G) */
287 #define LEADING_BYTE_CHINESE_CNS11643_1         171
288
289 /* Chinese CNS11643 Set 2       2/4 2/{8,9,10,11} 4/8 (H) */
290 #define LEADING_BYTE_CHINESE_CNS11643_2         172
291
292 /* Chinese CNS11643 Set 3       2/4 2/{8,9,10,11} 4/9 (I) */
293 #define LEADING_BYTE_CHINESE_CNS11643_3         183
294
295 /* Chinese CNS11643 Set 4       2/4 2/{8,9,10,11} 4/10 (J) */
296 #define LEADING_BYTE_CHINESE_CNS11643_4         184
297
298 /* Chinese CNS11643 Set 5       2/4 2/{8,9,10,11} 4/11 (K) */
299 #define LEADING_BYTE_CHINESE_CNS11643_5         185
300
301 /* Chinese CNS11643 Set 6       2/4 2/{8,9,10,11} 4/12 (L) */
302 #define LEADING_BYTE_CHINESE_CNS11643_6         186
303
304 /* Chinese CNS11643 Set 7       2/4 2/{8,9,10,11} 4/13 (M) */
305 #define LEADING_BYTE_CHINESE_CNS11643_7         187
306
307 /* DPRK Hangul KPS 9566-1997    2/4 2/{8,9,10,11} 4/14 (N) */
308 #define LEADING_BYTE_KOREAN_KPS9566             202
309
310
311 #define NUM_LEADING_BYTES       (80 * 3 - MIN_LEADING_BYTE)
312
313 \f
314 /************************************************************************/
315 /*            Information about a particular character set              */
316 /************************************************************************/
317
318 struct Lisp_Charset
319 {
320   struct lcrecord_header header;
321
322   int id;
323   Lisp_Object name;
324   Lisp_Object doc_string;
325   Lisp_Object registry;
326   Lisp_Object short_name;
327   Lisp_Object long_name;
328
329   Lisp_Object reverse_direction_charset;
330
331   Lisp_Object ccl_program;
332
333   /* Final byte of this character set in ISO2022 designating escape sequence */
334   Bufbyte final;
335
336   /* Number of columns a character in this charset takes up, on TTY
337      devices.  Not used for X devices. */
338   unsigned int columns;
339
340   /* Direction of this character set */
341   unsigned int direction;
342
343   /* Number of bytes used in encoding of this character set (1 .. 4) */
344   unsigned short dimension;
345
346   /* Number of chars in each dimension (94, 96, 128, 256) */
347   unsigned short chars;
348
349   /* Which half of font to be used to display this character set */
350   unsigned int graphic;
351
352   /* Code-point->character mapping table */
353   Lisp_Object decoding_table;
354
355   /* Character->code-point mapping table */
356   Lisp_Object encoding_table;
357
358   /* Range of character code */
359   Emchar ucs_min, ucs_max;
360
361   /* Offset for external code */
362   Emchar code_offset;
363
364   /* Offset for each byte */
365   Emchar byte_offset;
366 };
367 typedef struct Lisp_Charset Lisp_Charset;
368
369 DECLARE_LRECORD (charset, Lisp_Charset);
370 #define XCHARSET(x) XRECORD (x, charset, Lisp_Charset)
371 #define XSETCHARSET(x, p) XSETRECORD (x, p, charset)
372 #define CHARSETP(x) RECORDP (x, charset)
373 #define GC_CHARSETP(x) GC_RECORDP (x, charset)
374 #define CHECK_CHARSET(x) CHECK_RECORD (x, charset)
375 #define CONCHECK_CHARSET(x) CONCHECK_RECORD (x, charset)
376
377 #define CHARSET_LEFT_TO_RIGHT   0
378 #define CHARSET_RIGHT_TO_LEFT   1
379
380 /* Leading byte and id have been regrouped. -- OG */
381 #define CHARSET_ID(cs)           ((cs)->id)
382 #define CHARSET_LEADING_BYTE(cs) (CHARSET_ID(cs))
383 #define CHARSET_NAME(cs)         ((cs)->name)
384 #define CHARSET_SHORT_NAME(cs)   ((cs)->short_name)
385 #define CHARSET_LONG_NAME(cs)    ((cs)->long_name)
386 #define CHARSET_COLUMNS(cs)      ((cs)->columns)
387 #define CHARSET_GRAPHIC(cs)      ((cs)->graphic)
388 #define CHARSET_DIRECTION(cs)    ((cs)->direction)
389 #define CHARSET_FINAL(cs)        ((cs)->final)
390 #define CHARSET_DOC_STRING(cs)   ((cs)->doc_string)
391 #define CHARSET_REGISTRY(cs)     ((cs)->registry)
392 #define CHARSET_CCL_PROGRAM(cs)  ((cs)->ccl_program)
393 #define CHARSET_DIMENSION(cs)    ((cs)->dimension)
394 #define CHARSET_CHARS(cs)        ((cs)->chars)
395 #define CHARSET_REVERSE_DIRECTION_CHARSET(cs) ((cs)->reverse_direction_charset)
396 #define CHARSET_DECODING_TABLE(cs) ((cs)->decoding_table)
397 #define CHARSET_ENCODING_TABLE(cs) ((cs)->encoding_table)
398 #define CHARSET_UCS_MIN(cs)      ((cs)->ucs_min)
399 #define CHARSET_UCS_MAX(cs)      ((cs)->ucs_max)
400 #define CHARSET_CODE_OFFSET(cs)  ((cs)->code_offset)
401 #define CHARSET_BYTE_OFFSET(cs)  ((cs)->byte_offset)
402
403
404 #define XCHARSET_ID(cs)           CHARSET_ID           (XCHARSET (cs))
405 #define XCHARSET_NAME(cs)         CHARSET_NAME         (XCHARSET (cs))
406 #define XCHARSET_SHORT_NAME(cs)   CHARSET_SHORT_NAME   (XCHARSET (cs))
407 #define XCHARSET_LONG_NAME(cs)    CHARSET_LONG_NAME    (XCHARSET (cs))
408 #define XCHARSET_GRAPHIC(cs)      CHARSET_GRAPHIC      (XCHARSET (cs))
409 #define XCHARSET_DIRECTION(cs)    CHARSET_DIRECTION    (XCHARSET (cs))
410 #define XCHARSET_FINAL(cs)        CHARSET_FINAL        (XCHARSET (cs))
411 #define XCHARSET_DOC_STRING(cs)   CHARSET_DOC_STRING   (XCHARSET (cs))
412 #define XCHARSET_REGISTRY(cs)     CHARSET_REGISTRY     (XCHARSET (cs))
413 #define XCHARSET_LEADING_BYTE(cs) CHARSET_LEADING_BYTE (XCHARSET (cs))
414 #define XCHARSET_CCL_PROGRAM(cs)  CHARSET_CCL_PROGRAM  (XCHARSET (cs))
415 #define XCHARSET_DIMENSION(cs)    CHARSET_DIMENSION    (XCHARSET (cs))
416 #define XCHARSET_CHARS(cs)        CHARSET_CHARS        (XCHARSET (cs))
417 #define XCHARSET_REVERSE_DIRECTION_CHARSET(cs) \
418   CHARSET_REVERSE_DIRECTION_CHARSET (XCHARSET (cs))
419 #define XCHARSET_DECODING_TABLE(cs) CHARSET_DECODING_TABLE(XCHARSET(cs))
420 #define XCHARSET_ENCODING_TABLE(cs) CHARSET_ENCODING_TABLE(XCHARSET(cs))
421 #define XCHARSET_UCS_MIN(cs)      CHARSET_UCS_MIN(XCHARSET(cs))
422 #define XCHARSET_UCS_MAX(cs)      CHARSET_UCS_MAX(XCHARSET(cs))
423 #define XCHARSET_CODE_OFFSET(cs)  CHARSET_CODE_OFFSET(XCHARSET(cs))
424 #define XCHARSET_BYTE_OFFSET(cs)  CHARSET_BYTE_OFFSET(XCHARSET(cs))
425
426 struct charset_lookup {
427   /* Table of charsets indexed by (leading byte - 128). */
428   Lisp_Object charset_by_leading_byte[NUM_LEADING_BYTES];
429   
430   /* Table of charsets indexed by type/final-byte/direction. */
431   Lisp_Object charset_by_attributes[4][128];
432
433   Charset_ID next_allocated_leading_byte;
434 };
435
436 extern struct charset_lookup *chlook;
437
438 #ifdef ERROR_CHECK_TYPECHECK
439 /* int not Bufbyte even though that is the actual type of a leading byte.
440    This way, out-ot-range values will get caught rather than automatically
441    truncated. */
442 INLINE_HEADER Lisp_Object CHARSET_BY_LEADING_BYTE (Charset_ID lb);
443 INLINE_HEADER Lisp_Object
444 CHARSET_BY_LEADING_BYTE (Charset_ID lb)
445 {
446   assert (lb >= MIN_LEADING_BYTE &&
447           lb < (MIN_LEADING_BYTE + NUM_LEADING_BYTES));
448   return chlook->charset_by_leading_byte[lb - MIN_LEADING_BYTE];
449 }
450
451 #else
452
453 #define CHARSET_BY_LEADING_BYTE(lb) \
454   (chlook->charset_by_leading_byte[(lb) - MIN_LEADING_BYTE])
455
456 #endif
457
458 INLINE_HEADER Lisp_Object
459 CHARSET_BY_ATTRIBUTES (int chars, int dimension, int final, int dir);
460 INLINE_HEADER Lisp_Object
461 CHARSET_BY_ATTRIBUTES (int chars, int dimension, int final, int dir)
462 {
463   int type;
464
465   if (chars == 94)
466     type = 0;
467   else if (chars == 96)
468     type = 1;
469   else
470     return Qnil;
471   type += dimension == 1 ? 0 : 2;
472   return chlook->charset_by_attributes[type][final];
473 }
474
475 \f
476 /************************************************************************/
477 /*                        Dealing with characters                       */
478 /************************************************************************/
479
480 #define MAX_CHAR_BASIC_LATIN    0x007F
481
482 /*
483 #define MIN_CHAR_HEBREW         0x0590
484 #define MAX_CHAR_HEBREW         0x05EF
485 */
486
487 #define MIN_CHAR_THAI           0x0E00
488 #define MAX_CHAR_THAI           0x0E5F
489
490 /*
491 #define MIN_CHAR_HIRAGANA       0x3041
492 #define MAX_CHAR_HIRAGANA       0x3093
493
494 #define MIN_CHAR_KATAKANA       0x30A1
495 #define MAX_CHAR_KATAKANA       0x30F6
496 */
497
498 #define MIN_CHAR_HALFWIDTH_KATAKANA     0xFF61
499 #define MAX_CHAR_HALFWIDTH_KATAKANA     0xFF9F
500
501 #define MAX_CHAR_BMP            0x00FFFF
502
503 #define MIN_CHAR_DAIKANWA       0x00E00000
504 #define MAX_CHAR_DAIKANWA       (MIN_CHAR_DAIKANWA + 50100) /* 0xE0FFFF */
505 #define MIN_CHAR_MOJIKYO_0      MIN_CHAR_DAIKANWA
506 #define MAX_CHAR_MOJIKYO_0      (MIN_CHAR_MOJIKYO_0 + 94 * 60 * 22)
507
508 #define MIN_CHAR_94             0xE90940
509 #define MAX_CHAR_94             (MIN_CHAR_94 + 94 * 80 - 1)
510 #define MIN_CHAR_96             (MIN_CHAR_94 + 94 * 80)
511 #define MAX_CHAR_96             (MIN_CHAR_96 + 96 * 80 - 1)
512
513 #define MIN_CHAR_94x94          0xE9F6C0
514 #define MIN_CHAR_JIS_X0208_1990 (MIN_CHAR_94x94 + 94 * 94 * 79)
515 #define MAX_CHAR_94x94          (MIN_CHAR_94x94 + 94 * 94 * 80 - 1)
516 #define MAX_CHAR_JIS_X0208_1990 MAX_CHAR_94x94
517 #define MIN_CHAR_96x96          0xF4C000
518 #define MAX_CHAR_96x96          (MIN_CHAR_96x96 + 96 * 96 * 80 - 1)
519
520 #define MIN_CHAR_MOJIKYO        0x60000000
521 #define MAX_CHAR_MOJIKYO        (MIN_CHAR_MOJIKYO + 94 * 60 * 22)
522 #define MIN_CHAR_GT             0x61000000
523 #define MAX_CHAR_GT             (MIN_CHAR_GT + 66773)
524
525 Emchar decode_builtin_char (Lisp_Object charset, int code_point);
526
527 INLINE_HEADER int
528 DECODE_MOJIKYO_2022 (unsigned char b1, unsigned char b2, unsigned char b3);
529 INLINE_HEADER int
530 DECODE_MOJIKYO_2022 (unsigned char b1, unsigned char b2, unsigned char b3)
531 {
532   if (b2 < 16 + 32)
533     return 0;
534   else if (b2 < 16 + 32 + 30)
535     return
536       (b1 - 33) * (94 * 60)
537       + (b2 - (16 + 32)) * 94
538       + (b3 - 33)
539       + 1;
540   else if (b2 < 18 + 32 + 30)
541     return 0;
542   else if (b2 < 18 + 32 + 60)
543     return
544       (b1 - 33) * (94 * 60)
545       + (b2 - (18 + 32)) * 94
546       + (b3 - 33)
547       + 1;
548   else
549     return 0;
550 }
551
552 extern Lisp_Object Vcharset_chinese_big5;
553 extern Lisp_Object Vcharset_chinese_big5_1;
554 extern Lisp_Object Vcharset_chinese_big5_2;
555
556 INLINE_HEADER Emchar DECODE_CHAR (Lisp_Object charset, int code_point);
557 INLINE_HEADER Emchar
558 DECODE_CHAR (Lisp_Object charset, int code_point)
559 {
560   int dim = XCHARSET_DIMENSION (charset);
561   Lisp_Object decoding_table = XCHARSET_DECODING_TABLE (charset);
562   int idx;
563   Lisp_Object ch;
564
565   while (dim > 0)
566     {
567       dim--;
568       if ( VECTORP (decoding_table)
569            && ( 0 <= (idx = ((code_point >> (dim * 8))
570                              & 255) - XCHARSET_BYTE_OFFSET (charset)) )
571            && ( idx < XVECTOR_LENGTH (decoding_table) )
572            && !NILP (ch = XVECTOR_DATA(decoding_table)[idx]) )
573         {
574           if (CHARP (ch))
575             return XCHAR (ch);
576           else
577             decoding_table = ch;
578         }
579       else
580         break;
581     }
582   if (XCHARSET_DIMENSION (charset) == 1)
583     return decode_builtin_char (charset, code_point);
584   else if (EQ (charset, Vcharset_chinese_big5_1))
585     {
586       unsigned int I
587         = ((code_point >> 8) - 33) * (0xFF - 0xA1)
588         + ((code_point & 0xFF) - 33);
589       unsigned char b1 = I / (0xFF - 0xA1 + 0x7F - 0x40) + 0xA1;
590       unsigned char b2 = I % (0xFF - 0xA1 + 0x7F - 0x40);
591
592       b2 += b2 < 0x3F ? 0x40 : 0x62;
593       return DECODE_CHAR (Vcharset_chinese_big5, (b1 << 8) | b2);
594     }
595   else if (EQ (charset, Vcharset_chinese_big5_2))
596     {
597       unsigned int I
598         = ((code_point >> 8) - 33) * (0xFF - 0xA1)
599         + ((code_point & 0xFF) - 33);
600       unsigned char b1, b2;
601
602       I += (0xFF - 0xA1 + 0x7F - 0x40) * (0xC9 - 0xA1);
603       b1 = I / (0xFF - 0xA1 + 0x7F - 0x40) + 0xA1;
604       b2 = I % (0xFF - 0xA1 + 0x7F - 0x40);
605       b2 += b2 < 0x3F ? 0x40 : 0x62;
606       return DECODE_CHAR (Vcharset_chinese_big5, (b1 << 8) | b2);
607     }
608   else if (EQ (charset, Vcharset_mojikyo_2022_1))
609     {
610       int m =
611         DECODE_MOJIKYO_2022
612         ((unsigned char)(code_point >> 16),
613          (unsigned char)(code_point >>  8),
614          (unsigned char) code_point);
615
616       if (m > 0)
617         return DECODE_CHAR (Vcharset_mojikyo, m);
618       else
619         return ' ';
620     }
621   else
622     {
623       int plane = LEADING_BYTE_MOJIKYO_PJ_1 - XCHARSET_ID (charset);
624
625       if ( (0 <= plane) && (plane <= 21) )
626         {
627           int m = DECODE_MOJIKYO_2022 (plane + 33,
628                                        code_point >> 8,
629                                        (unsigned char)code_point);
630           if (m > 0)
631             return DECODE_CHAR (Vcharset_mojikyo, m);
632           else
633             return ' ';
634         }
635       else
636         return decode_builtin_char (charset, code_point);
637     }
638 }
639
640 /* Return a character whose charset is CHARSET and position-codes
641    are C1 and C2.  TYPE9N character ignores C2. */
642
643 INLINE_HEADER Emchar MAKE_CHAR (Lisp_Object charset, int c1, int c2);
644 INLINE_HEADER Emchar
645 MAKE_CHAR (Lisp_Object charset, int c1, int c2)
646 {
647   if (XCHARSET_DIMENSION (charset) == 1)
648     return DECODE_CHAR (charset, c1);
649   else
650     return DECODE_CHAR (charset, (c1 << 8) | c2);
651 }
652
653 extern Lisp_Object Vcharacter_attribute_table;
654
655 int encode_builtin_char_1 (Emchar c, Lisp_Object* charset);
656 int range_charset_code_point (Lisp_Object charset, Emchar ch);
657
658 INLINE_HEADER int charset_code_point (Lisp_Object charset, Emchar ch);
659 INLINE_HEADER int
660 charset_code_point (Lisp_Object charset, Emchar ch)
661 {
662   Lisp_Object encoding_table = XCHARSET_ENCODING_TABLE (charset);
663   Lisp_Object ret;
664
665   if ( CHAR_ID_TABLE_P (encoding_table)
666        && INTP (ret = get_char_id_table (ch, encoding_table)) )
667     return XINT (ret);
668   else
669     return range_charset_code_point (charset, ch);
670 }
671
672 extern Lisp_Object Vdefault_coded_charset_priority_list;
673 EXFUN (Ffind_charset, 1);
674
675 INLINE_HEADER int encode_char_1 (Emchar ch, Lisp_Object* charset);
676 INLINE_HEADER int
677 encode_char_1 (Emchar ch, Lisp_Object* charset)
678 {
679   Lisp_Object charsets = Vdefault_coded_charset_priority_list;
680
681   while (!NILP (charsets))
682     {
683       *charset = Ffind_charset (Fcar (charsets));
684       if (!NILP (*charset))
685         {
686           Lisp_Object encoding_table = XCHARSET_ENCODING_TABLE (*charset);
687           Lisp_Object ret;
688
689           if ( CHAR_ID_TABLE_P (encoding_table)
690                && INTP (ret = get_char_id_table (ch, encoding_table)) )
691             return XINT (ret);
692           else
693             {
694               int code_point = range_charset_code_point (*charset, ch);
695
696               if (code_point >= 0)
697                 return code_point;
698             }
699         }
700       charsets = Fcdr (charsets);             
701     }
702   
703   /* otherwise --- maybe for bootstrap */
704   return encode_builtin_char_1 (ch, charset);
705 }
706
707 #define ENCODE_CHAR(ch, charset)        encode_char_1 (ch, &(charset))
708
709 INLINE_HEADER void
710 breakup_char_1 (Emchar c, Lisp_Object *charset, int *c1, int *c2);
711 INLINE_HEADER void
712 breakup_char_1 (Emchar c, Lisp_Object *charset, int *c1, int *c2)
713 {
714   int code_point = encode_char_1 (c, charset);
715
716   if (code_point >= 0)
717     {
718       int dim = XCHARSET_DIMENSION (*charset);
719
720        if (dim == 1)
721          {
722            *c1 = code_point;
723            *c2 = 0;
724          }
725        else /* if (dim == 2) */
726          {
727            *c1 = code_point >> 8;
728            *c2 = code_point & 255;
729          }
730     }
731   else{
732     *c1 = *c2 = 0;
733   }
734 }
735
736 #define BREAKUP_CHAR(ch, charset, b1, b2) \
737   breakup_char_1 (ch, &(charset), &(b1), &(b2))
738
739 INLINE_HEADER Lisp_Object CHAR_CHARSET (Emchar ch);
740 INLINE_HEADER Lisp_Object
741 CHAR_CHARSET (Emchar ch)
742 {
743   Lisp_Object charset;
744  
745   ENCODE_CHAR (ch, charset);
746   return charset;
747 }
748
749 #define CHAR_CHARSET_ID(c)  (XCHARSET_ID(CHAR_CHARSET(c)))
750 #define CHAR_COLUMNS(c)     (CHARSET_COLUMNS(XCHARSET(CHAR_CHARSET(c))))
751
752
753 typedef struct Charc
754 {
755   Lisp_Object charset;
756   int code_point;
757 } Charc;
758
759 INLINE_HEADER Charc CHAR_TO_CHARC (Emchar ch);
760 INLINE_HEADER Charc
761 CHAR_TO_CHARC (Emchar ch)
762 {
763   Charc cc;
764
765   cc.code_point = encode_char_1 (ch, &cc.charset);
766   if (EQ (cc.charset, Vcharset_mojikyo))
767     {
768       int plane, byte1, byte2;
769
770       cc.code_point--;
771       plane = cc.code_point / (94 * 60);
772       byte1 = (cc.code_point % (94 * 60)) / 94;
773       if (byte1 < 30)
774         byte1 += 16 + 32;
775       else
776         byte1 += 18 + 32;
777       byte2 = cc.code_point % 94 + 33;
778       cc.charset
779         = CHARSET_BY_LEADING_BYTE (LEADING_BYTE_MOJIKYO_PJ_1 - plane);
780       cc.code_point = (byte1 << 8) | byte2;
781     }
782   return cc;
783 }
784
785 \f
786 /************************************************************************/
787 /*                            Exported functions                        */
788 /************************************************************************/
789
790 EXFUN (Fget_charset, 1);
791
792 extern Lisp_Object Qucs;
793
794 extern Lisp_Object Vcharset_japanese_jisx0208;
795 extern Lisp_Object Vcharset_japanese_jisx0208_1990;
796 extern Lisp_Object Vcharset_japanese_jisx0212;
797
798 Lisp_Object put_char_ccs_code_point (Lisp_Object character,
799                                      Lisp_Object ccs, Lisp_Object value);
800 Lisp_Object remove_char_ccs (Lisp_Object character, Lisp_Object ccs);
801
802 #endif /* INCLUDED_char_ucs_h_ */