91ab5ddd63a78735a3bb56b02419593945c17ad1
[chise/xemacs-chise.git-] / src / char-ucs.h
1 /* Header for UCS-4 character representation.
2    Copyright (C) 1999,2000 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 /* Rewritten by MORIOKA Tomohiko <tomo@m17n.org>. */
22
23 #ifndef _XEMACS_CHAR_UCS_H
24 #define _XEMACS_CHAR_UCS_H
25
26 #define valid_char_p(ch) 1
27
28 #define CHAR_ASCII_P(ch) ((ch) <= 0x7F)
29
30 \f
31 DECLARE_LRECORD (char_byte_table, struct Lisp_Char_Byte_Table);
32 #define XCHAR_BYTE_TABLE(x) \
33   XRECORD (x, char_byte_table, struct Lisp_Char_Byte_Table)
34 #define XSETCHAR_BYTE_TABLE(x, p) XSETRECORD (x, p, char_byte_table)
35 #define CHAR_BYTE_TABLE_P(x) RECORDP (x, char_byte_table)
36 #define GC_CHAR_BYTE_TABLE_P(x) GC_RECORDP (x, char_byte_table)
37 /* #define CHECK_CHAR_BYTE_TABLE(x) CHECK_RECORD (x, char_byte_table)
38    char table entries should never escape to Lisp */
39
40 struct Lisp_Char_Byte_Table
41 {
42   struct lcrecord_header header;
43
44   Lisp_Object property[256];
45 };
46
47
48 DECLARE_LRECORD (char_code_table, struct Lisp_Char_Code_Table);
49 #define XCHAR_CODE_TABLE(x) \
50   XRECORD (x, char_code_table, struct Lisp_Char_Code_Table)
51 #define XSETCHAR_CODE_TABLE(x, p) XSETRECORD (x, p, char_code_table)
52 #define CHAR_CODE_TABLE_P(x) RECORDP (x, char_code_table)
53 #define GC_CHAR_CODE_TABLE_P(x) GC_RECORDP (x, char_code_table)
54 /* #define CHECK_CHAR_CODE_TABLE(x) CHECK_RECORD (x, char_code_table)
55    char table entries should never escape to Lisp */
56
57 struct Lisp_Char_Code_Table
58 {
59   struct lcrecord_header header;
60
61   Lisp_Object table;
62 };
63
64 Lisp_Object get_char_code_table (Emchar ch, Lisp_Object table);
65
66
67 extern Lisp_Object Vcharset_ucs_bmp;
68 extern Lisp_Object Vcharset_latin_jisx0201;
69 extern Lisp_Object Vcharset_latin_iso8859_2;
70 extern Lisp_Object Vcharset_latin_iso8859_3;
71 extern Lisp_Object Vcharset_latin_iso8859_4;
72 extern Lisp_Object Vcharset_latin_iso8859_9;
73 extern Lisp_Object Vcharset_latin_viscii_lower;
74 extern Lisp_Object Vcharset_latin_viscii_upper;
75
76 \f
77 /************************************************************************/
78 /*                    Definition of leading bytes                       */
79 /************************************************************************/
80
81 typedef int Charset_ID;
82
83 #define MIN_LEADING_BYTE                -0x60
84 #define CHARSET_ID_OFFSET               0x00
85
86 /* represent normal 80-9F */
87 #define LEADING_BYTE_CONTROL_1          (CHARSET_ID_OFFSET - 1)
88
89 /* ISO/IEC 10646 BMP */
90 #define LEADING_BYTE_UCS_BMP            (CHARSET_ID_OFFSET - 2)
91
92 /* VISCII 1.1 */
93 #define LEADING_BYTE_LATIN_VISCII       (CHARSET_ID_OFFSET - 3)
94
95 /* MULE VISCII-LOWER                    (CHARSET_ID_OFFSET_96 + '1') */
96 #define LEADING_BYTE_LATIN_VISCII_LOWER (CHARSET_ID_OFFSET - 4)
97
98 /* MULE VISCII-UPPER                    (CHARSET_ID_OFFSET_96 + '2') */
99 #define LEADING_BYTE_LATIN_VISCII_UPPER (CHARSET_ID_OFFSET - 5)
100
101 /* Big5 Level 1                 2/4 2/{(8),9,10,11} 4/0 '0' */
102 #define LEADING_BYTE_CHINESE_BIG5_1     (CHARSET_ID_OFFSET - 6)
103
104 /* Big5 Level 2                 2/4 2/{(8),9,10,11} 4/0 '1' */
105 #define LEADING_BYTE_CHINESE_BIG5_2     (CHARSET_ID_OFFSET - 7)
106
107 #define LEADING_BYTE_ETHIOPIC_UCS       (CHARSET_ID_OFFSET - 8)
108
109 #define LEADING_BYTE_DAIKANWA           (CHARSET_ID_OFFSET - 9)
110
111 /* Japanese JIS X0208-1990      2/4 2/{(8),9,10,11} 4/2 (B) */
112 #define LEADING_BYTE_JAPANESE_JISX0208_1990 (CHARSET_ID_OFFSET - 10)
113
114 /* Konjaku-Mojikyo font (for pseudo-JIS X 0208 encoding) */
115 #define LEADING_BYTE_MOJIKYO_PJ_1       (CHARSET_ID_OFFSET - 11)
116 #define LEADING_BYTE_MOJIKYO_PJ_2       (CHARSET_ID_OFFSET - 12)
117 #define LEADING_BYTE_MOJIKYO_PJ_3       (CHARSET_ID_OFFSET - 13)
118 #define LEADING_BYTE_MOJIKYO_PJ_4       (CHARSET_ID_OFFSET - 14)
119 #define LEADING_BYTE_MOJIKYO_PJ_5       (CHARSET_ID_OFFSET - 15)
120 #define LEADING_BYTE_MOJIKYO_PJ_6       (CHARSET_ID_OFFSET - 16)
121 #define LEADING_BYTE_MOJIKYO_PJ_7       (CHARSET_ID_OFFSET - 17)
122 #define LEADING_BYTE_MOJIKYO_PJ_8       (CHARSET_ID_OFFSET - 18)
123 #define LEADING_BYTE_MOJIKYO_PJ_9       (CHARSET_ID_OFFSET - 19)
124 #define LEADING_BYTE_MOJIKYO_PJ_10      (CHARSET_ID_OFFSET - 20)
125 #define LEADING_BYTE_MOJIKYO_PJ_11      (CHARSET_ID_OFFSET - 21)
126 #define LEADING_BYTE_MOJIKYO_PJ_12      (CHARSET_ID_OFFSET - 22)
127 #define LEADING_BYTE_MOJIKYO_PJ_13      (CHARSET_ID_OFFSET - 23)
128 #define LEADING_BYTE_MOJIKYO_PJ_14      (CHARSET_ID_OFFSET - 24)
129 #define LEADING_BYTE_MOJIKYO_PJ_15      (CHARSET_ID_OFFSET - 25)
130 #define LEADING_BYTE_MOJIKYO_PJ_16      (CHARSET_ID_OFFSET - 26)
131 #define LEADING_BYTE_MOJIKYO_PJ_17      (CHARSET_ID_OFFSET - 27)
132 #define LEADING_BYTE_MOJIKYO_PJ_18      (CHARSET_ID_OFFSET - 28)
133 #define LEADING_BYTE_MOJIKYO_PJ_19      (CHARSET_ID_OFFSET - 29)
134 #define LEADING_BYTE_MOJIKYO_PJ_20      (CHARSET_ID_OFFSET - 30)
135 #define LEADING_BYTE_MOJIKYO_PJ_21      (CHARSET_ID_OFFSET - 31)
136
137 #define MIN_LEADING_BYTE_PRIVATE        MIN_LEADING_BYTE
138 #define MAX_LEADING_BYTE_PRIVATE        (CHARSET_ID_OFFSET - 32)
139
140
141 /* #define CHARSET_ID_OFFSET_94         (CHARSET_ID_OFFSET - '0') */
142
143 /* #define MIN_CHARSET_ID_PRIVATE_94    (CHARSET_ID_OFFSET_94 + '0') */
144 /* #define MAX_CHARSET_ID_PRIVATE_94    (CHARSET_ID_OFFSET_94 + '?') */
145
146 /* ISO 646 IRV */
147 #define LEADING_BYTE_ASCII                6 /* (CHARSET_ID_OFFSET_94 + 'B') */
148
149 /* Right half of JIS X0201-1976 */
150 #define LEADING_BYTE_KATAKANA_JISX0201   13 /* (CHARSET_ID_OFFSET_94 + 'I') */
151
152 /* Left  half of JIS X0201-1976 */
153 #define LEADING_BYTE_LATIN_JISX0201      14 /* (CHARSET_ID_OFFSET_94 + 'J') */
154
155
156 /* #define CHARSET_ID_OFFSET_96         (CHARSET_ID_OFFSET_94 + 80) */
157
158 /* Right half of ISO 8859-1 */
159 #define LEADING_BYTE_LATIN_ISO8859_1    100 /* (CHARSET_ID_OFFSET_96 + 'A') */
160
161 /* Right half of ISO 8859-2 */
162 #define LEADING_BYTE_LATIN_ISO8859_2    101 /* (CHARSET_ID_OFFSET_96 + 'B') */
163
164 /* Right half of ISO 8859-3 */
165 #define LEADING_BYTE_LATIN_ISO8859_3    109 /* (CHARSET_ID_OFFSET_96 + 'C') */
166
167 /* Right half of ISO 8859-4 */
168 #define LEADING_BYTE_LATIN_ISO8859_4    110 /* (CHARSET_ID_OFFSET_96 + 'D') */
169
170 /* Right half of ISO 8859-7 */
171 #define LEADING_BYTE_GREEK_ISO8859_7    126 /* (CHARSET_ID_OFFSET_96 + 'F') */
172
173 /* Right half of ISO 8859-6 */
174 #define LEADING_BYTE_ARABIC_ISO8859_6   127 /* (CHARSET_ID_OFFSET_96 + 'G') */
175
176 /* Right half of ISO 8859-8 */
177 #define LEADING_BYTE_HEBREW_ISO8859_8   138 /* (CHARSET_ID_OFFSET_96 + 'H') */
178
179 /* Right half of ISO 8859-5 */
180 #define LEADING_BYTE_CYRILLIC_ISO8859_5 144 /* (CHARSET_ID_OFFSET_96 + 'L') */
181
182 /* Right half of ISO 8859-9 */
183 #define LEADING_BYTE_LATIN_ISO8859_9    148 /* (CHARSET_ID_OFFSET_96 + 'M') */
184
185 /* TIS620-2533 */
186 #define LEADING_BYTE_THAI_TIS620        166 /* (CHARSET_ID_OFFSET_96 + 'T') */
187
188
189 /* #define CHARSET_ID_OFFSET_94x94              (CHARSET_ID_OFFSET_96 + 80) */
190
191
192 /* #define MIN_LEADING_BYTE_PRIVATE_2   ('0' + CHARSET_ID_OFFSET_94x94) */
193 /* #define MAX_LEADING_BYTE_PRIVATE_2   ('?' + CHARSET_ID_OFFSET_94x94) */
194
195 /* Japanese JIS X0208-1978      2/4 2/{(8),9,10,11} 4/0 (@) */
196 #define LEADING_BYTE_JAPANESE_JISX0208_1978      42
197
198 /* Chinese Hanzi GB2312-1980    2/4 2/{(8),9,10,11} 4/1 (A) */
199 #define LEADING_BYTE_CHINESE_GB2312              58
200
201 /* Japanese JIS X0208-1983      2/4 2/{(8),9,10,11} 4/2 (B) */
202 #define LEADING_BYTE_JAPANESE_JISX0208           87
203
204 /* Hangul KS C5601-1987         2/4 2/{8,9,10,11} 4/3 (C) */
205 #define LEADING_BYTE_KOREAN_KSC5601             149
206
207 /* Japanese JIS X0212-1990      2/4 2/{8,9,10,11} 4/4 (D) */
208 #define LEADING_BYTE_JAPANESE_JISX0212          159
209
210 /* CCITT Extended GB            2/4 2/{8,9,10,11} 4/5 (E) */
211 #define LEADING_BYTE_CHINESE_CCITT_GB           165
212
213 /* Chinese CNS11643 Set 1       2/4 2/{8,9,10,11} 4/7 (G) */
214 #define LEADING_BYTE_CHINESE_CNS11643_1         171
215
216 /* Chinese CNS11643 Set 2       2/4 2/{8,9,10,11} 4/8 (H) */
217 #define LEADING_BYTE_CHINESE_CNS11643_2         172
218
219 /* Chinese CNS11643 Set 3       2/4 2/{8,9,10,11} 4/9 (I) */
220 #define LEADING_BYTE_CHINESE_CNS11643_3         183
221
222 /* Chinese CNS11643 Set 4       2/4 2/{8,9,10,11} 4/10 (J) */
223 #define LEADING_BYTE_CHINESE_CNS11643_4         184
224
225 /* Chinese CNS11643 Set 5       2/4 2/{8,9,10,11} 4/11 (K) */
226 #define LEADING_BYTE_CHINESE_CNS11643_5         185
227
228 /* Chinese CNS11643 Set 6       2/4 2/{8,9,10,11} 4/12 (L) */
229 #define LEADING_BYTE_CHINESE_CNS11643_6         186
230
231 /* Chinese CNS11643 Set 7       2/4 2/{8,9,10,11} 4/13 (M) */
232 #define LEADING_BYTE_CHINESE_CNS11643_7         187
233
234 /* DPRK Hangul KPS 9566-1997    2/4 2/{8,9,10,11} 4/14 (N) */
235 #define LEADING_BYTE_KOREAN_KPS9566             202
236
237
238 #define NUM_LEADING_BYTES       (80 * 3 - MIN_LEADING_BYTE)
239
240 \f
241 /************************************************************************/
242 /*            Information about a particular character set              */
243 /************************************************************************/
244
245 struct Lisp_Charset
246 {
247   struct lcrecord_header header;
248
249   int id;
250   Lisp_Object name;
251   Lisp_Object doc_string, registry, short_name, long_name;
252
253   Lisp_Object reverse_direction_charset;
254
255   Lisp_Object ccl_program;
256
257   /* Final byte of this character set in ISO2022 designating escape sequence */
258   Bufbyte final;
259
260   /* Number of columns a character in this charset takes up, on TTY
261      devices.  Not used for X devices. */
262   unsigned int columns;
263
264   /* Direction of this character set */
265   unsigned int direction;
266
267   /* Type of this character set (94, 96, 94x94, 96x96) */
268   unsigned int type;
269
270   /* Number of bytes used in encoding of this character set (1 or 2) */
271   unsigned int dimension;
272
273   /* Number of chars in each dimension (usually 94 or 96) */
274   unsigned int chars;
275
276   /* Which half of font to be used to display this character set */
277   unsigned int graphic;
278
279   /* Byte->character mapping table */
280   Lisp_Object decoding_table;
281
282   /* Range of character code */
283   Emchar ucs_min, ucs_max;
284
285   /* Offset for external code */
286   Emchar code_offset;
287
288   /* Offset for each byte */
289   Emchar byte_offset;
290 };
291
292 DECLARE_LRECORD (charset, struct Lisp_Charset);
293 #define XCHARSET(x) XRECORD (x, charset, struct Lisp_Charset)
294 #define XSETCHARSET(x, p) XSETRECORD (x, p, charset)
295 #define CHARSETP(x) RECORDP (x, charset)
296 #define GC_CHARSETP(x) GC_RECORDP (x, charset)
297 #define CHECK_CHARSET(x) CHECK_RECORD (x, charset)
298 #define CONCHECK_CHARSET(x) CONCHECK_RECORD (x, charset)
299
300 #define CHARSET_TYPE_94      0 /* This charset includes 94      characters. */
301 #define CHARSET_TYPE_94X94   1 /* This charset includes 94x94   characters. */
302 #define CHARSET_TYPE_96      2 /* This charset includes 96      characters. */
303 #define CHARSET_TYPE_96X96   3 /* This charset includes 96x96   characters. */
304 #define CHARSET_TYPE_128     4 /* This charset includes 128     characters. */
305 #define CHARSET_TYPE_128X128 5 /* This charset includes 128x128 characters. */
306 #define CHARSET_TYPE_256     6 /* This charset includes 256     characters. */
307 #define CHARSET_TYPE_256X256 7 /* This charset includes 256x256 characters. */
308
309 #define CHARSET_LEFT_TO_RIGHT   0
310 #define CHARSET_RIGHT_TO_LEFT   1
311
312 /* Leading byte and id have been regrouped. -- OG */
313 #define CHARSET_ID(cs)           ((cs)->id)
314 #define CHARSET_LEADING_BYTE(cs) (CHARSET_ID(cs))
315 #define CHARSET_NAME(cs)         ((cs)->name)
316 #define CHARSET_SHORT_NAME(cs)   ((cs)->short_name)
317 #define CHARSET_LONG_NAME(cs)    ((cs)->long_name)
318 #define CHARSET_COLUMNS(cs)      ((cs)->columns)
319 #define CHARSET_GRAPHIC(cs)      ((cs)->graphic)
320 #define CHARSET_TYPE(cs)         ((cs)->type)
321 #define CHARSET_DIRECTION(cs)    ((cs)->direction)
322 #define CHARSET_FINAL(cs)        ((cs)->final)
323 #define CHARSET_DOC_STRING(cs)   ((cs)->doc_string)
324 #define CHARSET_REGISTRY(cs)     ((cs)->registry)
325 #define CHARSET_CCL_PROGRAM(cs)  ((cs)->ccl_program)
326 #define CHARSET_DIMENSION(cs)    ((cs)->dimension)
327 #define CHARSET_CHARS(cs)        ((cs)->chars)
328 #define CHARSET_REVERSE_DIRECTION_CHARSET(cs) ((cs)->reverse_direction_charset)
329 #define CHARSET_DECODING_TABLE(cs) ((cs)->decoding_table)
330 #define CHARSET_UCS_MIN(cs)      ((cs)->ucs_min)
331 #define CHARSET_UCS_MAX(cs)      ((cs)->ucs_max)
332 #define CHARSET_CODE_OFFSET(cs)  ((cs)->code_offset)
333 #define CHARSET_BYTE_OFFSET(cs)  ((cs)->byte_offset)
334
335
336 #define XCHARSET_ID(cs)           CHARSET_ID           (XCHARSET (cs))
337 #define XCHARSET_NAME(cs)         CHARSET_NAME         (XCHARSET (cs))
338 #define XCHARSET_SHORT_NAME(cs)   CHARSET_SHORT_NAME   (XCHARSET (cs))
339 #define XCHARSET_LONG_NAME(cs)    CHARSET_LONG_NAME    (XCHARSET (cs))
340 #define XCHARSET_GRAPHIC(cs)      CHARSET_GRAPHIC      (XCHARSET (cs))
341 #define XCHARSET_TYPE(cs)         CHARSET_TYPE         (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_REVERSE_DIRECTION_CHARSET(cs) \
351   CHARSET_REVERSE_DIRECTION_CHARSET (XCHARSET (cs))
352 #define XCHARSET_DECODING_TABLE(cs) CHARSET_DECODING_TABLE(XCHARSET(cs))
353 #define XCHARSET_UCS_MIN(cs)      CHARSET_UCS_MIN(XCHARSET(cs))
354 #define XCHARSET_UCS_MAX(cs)      CHARSET_UCS_MAX(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
358 struct charset_lookup {
359   /* Table of charsets indexed by (leading byte - 128). */
360   Lisp_Object charset_by_leading_byte[NUM_LEADING_BYTES];
361   
362   /* Table of charsets indexed by type/final-byte/direction. */
363   Lisp_Object charset_by_attributes[4][128];
364 };
365
366 extern struct charset_lookup *chlook;
367
368 #ifdef ERROR_CHECK_TYPECHECK
369 /* int not Bufbyte even though that is the actual type of a leading byte.
370    This way, out-ot-range values will get caught rather than automatically
371    truncated. */
372 INLINE Lisp_Object CHARSET_BY_LEADING_BYTE (Charset_ID lb);
373 INLINE Lisp_Object
374 CHARSET_BY_LEADING_BYTE (Charset_ID lb)
375 {
376   assert (lb >= MIN_LEADING_BYTE &&
377           lb < (MIN_LEADING_BYTE + NUM_LEADING_BYTES));
378   return chlook->charset_by_leading_byte[lb - MIN_LEADING_BYTE];
379 }
380
381 #else
382
383 #define CHARSET_BY_LEADING_BYTE(lb) \
384   (chlook->charset_by_leading_byte[(lb) - MIN_LEADING_BYTE])
385
386 #endif
387
388 #define CHARSET_BY_ATTRIBUTES(type, final, dir) \
389   (chlook->charset_by_attributes[type][final])
390
391 \f
392 /************************************************************************/
393 /*                        Dealing with characters                       */
394 /************************************************************************/
395
396 #define MAX_CHAR_BASIC_LATIN    0x007F
397
398 #define MIN_CHAR_GREEK          0x0370
399 #define MAX_CHAR_GREEK          0x03CF
400
401 #define MIN_CHAR_CYRILLIC       0x0400
402 #define MAX_CHAR_CYRILLIC       0x045F
403
404 #define MIN_CHAR_HEBREW         0x0590
405 #define MAX_CHAR_HEBREW         0x05EF
406
407 #define MIN_CHAR_THAI           0x0E00
408 #define MAX_CHAR_THAI           0x0E5F
409
410 /*
411 #define MIN_CHAR_HIRAGANA       0x3041
412 #define MAX_CHAR_HIRAGANA       0x3093
413
414 #define MIN_CHAR_KATAKANA       0x30A1
415 #define MAX_CHAR_KATAKANA       0x30F6
416 */
417
418 #define MIN_CHAR_HALFWIDTH_KATAKANA     0xFF61
419 #define MAX_CHAR_HALFWIDTH_KATAKANA     0xFF9F
420
421 #define MAX_CHAR_BMP            0x00FFFF
422
423 #define MIN_CHAR_DAIKANWA       0xE00000
424 #define MAX_CHAR_DAIKANWA       0xE0FFFF
425
426 #define MIN_CHAR_94             0xE90940
427 #define MAX_CHAR_94             (MIN_CHAR_94 + 94 * 80 - 1)
428 #define MIN_CHAR_96             (MIN_CHAR_94 + 94 * 80)
429 #define MAX_CHAR_96             (MIN_CHAR_96 + 96 * 80 - 1)
430
431 #define MIN_CHAR_94x94          0xE9F6C0
432 #define MIN_CHAR_JIS_X0208_1990 (MIN_CHAR_94x94 + 94 * 94 * 79)
433 #define MAX_CHAR_94x94          (MIN_CHAR_94x94 + 94 * 94 * 80 - 1)
434 #define MAX_CHAR_JIS_X0208_1990 MAX_CHAR_94x94
435 #define MIN_CHAR_96x96          0xF4C000
436 #define MAX_CHAR_96x96          (MIN_CHAR_96x96 + 96 * 96 * 80 - 1)
437
438
439 /* Return a character whose charset is CHARSET and position-codes
440    are C1 and C2.  TYPE9N character ignores C2. */
441
442 INLINE Emchar MAKE_CHAR (Lisp_Object charset, int c1, int c2);
443 INLINE Emchar
444 MAKE_CHAR (Lisp_Object charset, int c1, int c2)
445 {
446   Lisp_Object decoding_table = XCHARSET_DECODING_TABLE (charset);
447   int idx;
448   Lisp_Object ch;
449
450   if (!EQ (decoding_table, Qnil)
451       && (0 <= (idx = c1 - XCHARSET_BYTE_OFFSET (charset)))
452       && (idx < XVECTOR_LENGTH (decoding_table))
453       && !EQ (ch = XVECTOR_DATA(decoding_table)[idx], Qnil))
454     {
455       if (VECTORP (ch))
456         {
457           if ((0 <= (idx = c2 - XCHARSET_BYTE_OFFSET (charset)))
458               && (idx < XVECTOR_LENGTH (ch))
459               && !EQ (ch = XVECTOR_DATA(ch)[idx], Qnil))
460             return XCHAR (ch);
461         }
462       else
463         return XCHAR (ch);
464     }
465   if (XCHARSET_UCS_MAX (charset))
466     {
467       Emchar code
468         = (XCHARSET_DIMENSION (charset) == 1
469            ?
470            c1 - XCHARSET_BYTE_OFFSET (charset)
471            :
472            (c1 - XCHARSET_BYTE_OFFSET (charset)) * XCHARSET_CHARS (charset)
473            + c2  - XCHARSET_BYTE_OFFSET (charset))
474         - XCHARSET_CODE_OFFSET (charset) + XCHARSET_UCS_MIN (charset);
475       if ((code < XCHARSET_UCS_MIN (charset))
476           || (XCHARSET_UCS_MAX (charset) < code))
477         signal_simple_error ("Arguments makes invalid character",
478                              make_char (code));
479       return code;
480     }
481   else if (XCHARSET_DIMENSION (charset) == 1)
482     {
483       switch (XCHARSET_CHARS (charset))
484         {
485         case 94:
486           return MIN_CHAR_94
487             + (XCHARSET_FINAL (charset) - '0') * 94 + (c1 - 33);
488         case 96:
489           return MIN_CHAR_96
490             + (XCHARSET_FINAL (charset) - '0') * 96 + (c1 - 32);
491         default:
492           abort ();
493         }
494     }
495   else
496     {
497       switch (XCHARSET_CHARS (charset))
498         {
499         case 94:
500           return MIN_CHAR_94x94
501             + (XCHARSET_FINAL (charset) - '0') * 94 * 94
502             + (c1 - 33) * 94 + (c2 - 33);
503         case 96:
504           return MIN_CHAR_96x96
505             + (XCHARSET_FINAL (charset) - '0') * 96 * 96
506             + (c1 - 32) * 96 + (c2 - 32);
507         default:
508           abort ();
509         }
510     }
511 }
512
513 extern Lisp_Object Vcharacter_attribute_table;
514
515 int encode_builtin_char_1 (Emchar c, Lisp_Object* charset);
516 int range_charset_code_point (Lisp_Object charset, Emchar ch);
517
518 INLINE int charset_code_point (Lisp_Object charset, Emchar ch);
519 INLINE int
520 charset_code_point (Lisp_Object charset, Emchar ch)
521 {
522   Lisp_Object cdef = get_char_code_table (ch, Vcharacter_attribute_table);
523
524   if (!NILP (cdef))
525     {
526       Lisp_Object field = Fassq (charset, cdef);
527
528       if (!NILP (field))
529         return XINT (Fcdr (field));
530     }
531   return range_charset_code_point (charset, ch);
532 }
533
534 extern Lisp_Object Vdefault_coded_charset_priority_list;
535 EXFUN (Ffind_charset, 1);
536
537 INLINE int encode_char_1 (Emchar c, Lisp_Object* charset);
538 INLINE int
539 encode_char_1 (Emchar c, Lisp_Object* charset)
540 {
541   Lisp_Object cdef = get_char_code_table (c, Vcharacter_attribute_table);
542
543   if (!EQ (cdef, Qnil))
544     {
545       Lisp_Object charsets = Vdefault_coded_charset_priority_list;
546       Lisp_Object field;
547
548       while (!EQ (charsets, Qnil))
549         {
550           int code_point;
551
552           *charset = Ffind_charset (Fcar (charsets));
553           if (!EQ (*charset, Qnil))
554             {
555               if (!NILP (field = Fassq (*charset, cdef)))
556                 return XINT (Fcdr (field));
557               else if ((code_point
558                         = range_charset_code_point (*charset, c)) >= 0)
559                 return code_point;
560             }
561           charsets = Fcdr (charsets);         
562         }
563     }
564   
565   /* otherwise --- maybe for bootstrap */
566   return encode_builtin_char_1 (c, charset);
567 }
568
569 #define ENCODE_CHAR(ch, charset)        encode_char_1 (ch, &(charset))
570
571 INLINE void breakup_char_1 (Emchar c, Lisp_Object *charset, int *c1, int *c2);
572 INLINE void
573 breakup_char_1 (Emchar c, Lisp_Object *charset, int *c1, int *c2)
574 {
575   int code_point = encode_char_1 (c, charset);
576
577   if (code_point >= 0)
578     {
579       int dim = XCHARSET_DIMENSION (*charset);
580
581        if (dim == 1)
582          {
583            *c1 = code_point;
584            *c2 = 0;
585          }
586        else /* if (dim == 2) */
587          {
588            *c1 = code_point >> 8;
589            *c2 = code_point & 255;
590          }
591     }
592   else{
593     *c1 = *c2 = 0;
594   }
595 }
596
597 #define BREAKUP_CHAR(ch, charset, b1, b2) \
598   breakup_char_1 (ch, &(charset), &(b1), &(b2))
599
600 INLINE Lisp_Object CHAR_CHARSET (Emchar ch);
601 INLINE Lisp_Object
602 CHAR_CHARSET (Emchar ch)
603 {
604   Lisp_Object charset;
605   int b1, b2;
606
607   BREAKUP_CHAR(ch, charset, b1, b2);
608   return charset;
609 }
610
611 #define CHAR_CHARSET_ID(c)  (XCHARSET_ID(CHAR_CHARSET(c)))
612 #define CHAR_COLUMNS(c)     (CHARSET_COLUMNS(XCHARSET(CHAR_CHARSET(c))))
613
614 \f
615 /************************************************************************/
616 /*                            Exported functions                        */
617 /************************************************************************/
618
619 EXFUN (Fget_charset, 1);
620
621 extern Lisp_Object Vcharset_chinese_big5_1;
622 extern Lisp_Object Vcharset_chinese_big5_2;
623 extern Lisp_Object Vcharset_japanese_jisx0208;
624 extern Lisp_Object Vcharset_japanese_jisx0212;
625
626 #endif /* _XEMACS_CHAR_UCS_H */