(XCHARSET_GET_BYTE1): New inline function.
[chise/xemacs-chise.git] / src / char-ucs.h
1 /* Header for UCS-4 character representation.
2    Copyright (C) 1999 Electrotechnical Laboratory, JAPAN.
3    Licensed to the Free Software Foundation.
4
5 This file is part of XEmacs.
6
7 XEmacs is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
11
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with XEmacs; see the file COPYING.  If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 /* Rewritten by MORIOKA Tomohiko <tomo@m17n.org>. */
23
24 #ifndef _XEMACS_CHAR_UCS_H
25 #define _XEMACS_CHAR_UCS_H
26
27 #define valid_char_p(ch) 1
28
29 #define CHAR_ASCII_P(ch) ((ch) <= 0x7F)
30
31 \f
32 typedef struct
33 {
34   unsigned char *base;
35   size_t  offset;
36   size_t  size;
37 } Emchar_to_byte_table;
38
39 unsigned char
40 get_byte_from_character_table (Emchar ch, Emchar_to_byte_table* table);
41
42
43 extern Lisp_Object Vcharset_ucs_bmp;
44 extern Lisp_Object Vcharset_latin_jisx0201;
45 extern Lisp_Object Vcharset_latin_iso8859_2;
46 extern Lisp_Object Vcharset_latin_iso8859_3;
47 extern Lisp_Object Vcharset_latin_iso8859_4;
48 extern Lisp_Object Vcharset_latin_iso8859_9;
49 extern Lisp_Object Vcharset_latin_viscii_lower;
50 extern Lisp_Object Vcharset_latin_viscii_upper;
51
52 \f
53 /************************************************************************/
54 /*                    Definition of leading bytes                       */
55 /************************************************************************/
56
57 typedef int Charset_ID;
58
59 #define MIN_LEADING_BYTE                0x80
60
61 #define LEADING_BYTE_UCS_BMP            0x80
62 #define LEADING_BYTE_CONTROL_1          0x81 /* represent normal 80-9F */
63
64
65 #define CHARSET_ID_OFFSET_94            0x55
66
67 #define MIN_CHARSET_ID_PRIVATE_94       (CHARSET_ID_OFFSET_94 + '0')
68 #define MAX_CHARSET_ID_PRIVATE_94       (CHARSET_ID_OFFSET_94 + '?')
69
70 /* ISO 646 IRV */
71 #define LEADING_BYTE_ASCII              (CHARSET_ID_OFFSET_94 + 'B')
72
73 /* Right half of JIS X0201-1976 */
74 #define LEADING_BYTE_KATAKANA_JISX0201  (CHARSET_ID_OFFSET_94 + 'I')
75
76 /* Left  half of JIS X0201-1976 */
77 #define LEADING_BYTE_LATIN_JISX0201     (CHARSET_ID_OFFSET_94 + 'J')
78
79
80 #define CHARSET_ID_OFFSET_96            0x70
81
82 #define LEADING_BYTE_LATIN_VISCII_LOWER (CHARSET_ID_OFFSET_96 + '1')
83 #define LEADING_BYTE_LATIN_VISCII_UPPER (CHARSET_ID_OFFSET_96 + '2')
84
85 /* Right half of ISO 8859-1 */
86 #define LEADING_BYTE_LATIN_ISO8859_1    (CHARSET_ID_OFFSET_96 + 'A')
87
88 /* Right half of ISO 8859-2 */
89 #define LEADING_BYTE_LATIN_ISO8859_2    (CHARSET_ID_OFFSET_96 + 'B')
90
91 /* Right half of ISO 8859-3 */
92 #define LEADING_BYTE_LATIN_ISO8859_3    (CHARSET_ID_OFFSET_96 + 'C')
93
94 /* Right half of ISO 8859-4 */
95 #define LEADING_BYTE_LATIN_ISO8859_4    (CHARSET_ID_OFFSET_96 + 'D')
96
97 /* Right half of ISO 8859-7 */
98 #define LEADING_BYTE_GREEK_ISO8859_7    (CHARSET_ID_OFFSET_96 + 'F')
99
100 /* Right half of ISO 8859-6 */
101 #define LEADING_BYTE_ARABIC_ISO8859_6   (CHARSET_ID_OFFSET_96 + 'G')
102
103 /* Right half of ISO 8859-8 */
104 #define LEADING_BYTE_HEBREW_ISO8859_8   (CHARSET_ID_OFFSET_96 + 'H')
105
106 /* Right half of ISO 8859-5 */
107 #define LEADING_BYTE_CYRILLIC_ISO8859_5 (CHARSET_ID_OFFSET_96 + 'L')
108
109 /* Right half of ISO 8859-9 */
110 #define LEADING_BYTE_LATIN_ISO8859_9    (CHARSET_ID_OFFSET_96 + 'M')
111
112 /* TIS620-2533 */
113 #define LEADING_BYTE_THAI_TIS620        (CHARSET_ID_OFFSET_96 + 'T')
114
115
116 #define MIN_LEADING_BYTE_PRIVATE_1      0xD0
117 #define MAX_LEADING_BYTE_PRIVATE_1      0xDF
118
119
120 #define CHARSET_ID_OFFSET_94x94         0xB0
121
122 /* Big5 Level 1 */
123 #define LEADING_BYTE_CHINESE_BIG5_1     ('0' + CHARSET_ID_OFFSET_94x94)
124
125 /* Big5 Level 2 */
126 #define LEADING_BYTE_CHINESE_BIG5_2     ('1' + CHARSET_ID_OFFSET_94x94)
127
128 #define MIN_LEADING_BYTE_PRIVATE_2      ('0' + CHARSET_ID_OFFSET_94x94)
129 #define MAX_LEADING_BYTE_PRIVATE_2      ('?' + CHARSET_ID_OFFSET_94x94)
130
131 /* Japanese JIS X0208-1978 */
132 #define LEADING_BYTE_JAPANESE_JISX0208_1978 \
133                                         ('@' + CHARSET_ID_OFFSET_94x94)
134
135 /* Chinese Hanzi GB2312-1980 */
136 #define LEADING_BYTE_CHINESE_GB2312     ('A' + CHARSET_ID_OFFSET_94x94)
137
138 /* Japanese JIS X0208-1983 */
139 #define LEADING_BYTE_JAPANESE_JISX0208  ('B' + CHARSET_ID_OFFSET_94x94)
140
141 /* Hangul KS C5601-1987 */
142 #define LEADING_BYTE_KOREAN_KSC5601     ('C' + CHARSET_ID_OFFSET_94x94)
143
144 /* Japanese JIS X0212-1990 */
145 #define LEADING_BYTE_JAPANESE_JISX0212  ('D' + CHARSET_ID_OFFSET_94x94)
146
147 /* CCITT Extended GB */
148 #define LEADING_BYTE_CHINESE_CCITT_GB   ('E' + CHARSET_ID_OFFSET_94x94)
149
150 /* Chinese CNS11643 Set 1 */
151 #define LEADING_BYTE_CHINESE_CNS11643_1 ('G' + CHARSET_ID_OFFSET_94x94)
152
153 /* Chinese CNS11643 Set 2 */
154 #define LEADING_BYTE_CHINESE_CNS11643_2 ('H' + CHARSET_ID_OFFSET_94x94)
155
156 /* Chinese CNS11643 Set 3 */
157 #define LEADING_BYTE_CHINESE_CNS11643_3 ('I' + CHARSET_ID_OFFSET_94x94)
158
159 /* Chinese CNS11643 Set 4 */
160 #define LEADING_BYTE_CHINESE_CNS11643_4 ('J' + CHARSET_ID_OFFSET_94x94)
161
162 /* Chinese CNS11643 Set 5 */
163 #define LEADING_BYTE_CHINESE_CNS11643_5 ('K' + CHARSET_ID_OFFSET_94x94)
164
165 /* Chinese CNS11643 Set 6 */
166 #define LEADING_BYTE_CHINESE_CNS11643_6 ('L' + CHARSET_ID_OFFSET_94x94)
167
168 /* Chinese CNS11643 Set 7 */
169 #define LEADING_BYTE_CHINESE_CNS11643_7 ('M' + CHARSET_ID_OFFSET_94x94)
170
171 /* DPRK Hangul KPS 9566-1997 */
172 #define LEADING_BYTE_KOREAN_KPS9566     ('N' + CHARSET_ID_OFFSET_94x94)
173
174 #define MIN_LEADING_BYTE_OFFICIAL_2     LEADING_BYTE_JAPANESE_JISX0208_1978
175 #define MAX_LEADING_BYTE_OFFICIAL_2     LEADING_BYTE_KOREAN_KPS9566
176
177 #define NUM_LEADING_BYTES 256
178
179 \f
180 /************************************************************************/
181 /*            Information about a particular character set              */
182 /************************************************************************/
183
184 struct Lisp_Charset
185 {
186   struct lcrecord_header header;
187
188   int id;
189   Lisp_Object name;
190   Lisp_Object doc_string, registry, short_name, long_name;
191
192   Lisp_Object reverse_direction_charset;
193
194   Lisp_Object ccl_program;
195
196   /* Final byte of this character set in ISO2022 designating escape sequence */
197   Bufbyte final;
198
199   /* Number of columns a character in this charset takes up, on TTY
200      devices.  Not used for X devices. */
201   unsigned int columns;
202
203   /* Direction of this character set */
204   unsigned int direction;
205
206   /* Type of this character set (94, 96, 94x94, 96x96) */
207   unsigned int type;
208
209   /* Number of bytes used in encoding of this character set (1 or 2) */
210   unsigned int dimension;
211
212   /* Number of chars in each dimension (usually 94 or 96) */
213   unsigned int chars;
214
215   /* Which half of font to be used to display this character set */
216   unsigned int graphic;
217
218   /* byte->character mapping table */
219   Emchar* decoding_table;
220
221   /* character->byte mapping table */
222   Emchar_to_byte_table* to_byte1_table;
223   Emchar_to_byte_table* to_byte2_table;
224 };
225
226 DECLARE_LRECORD (charset, struct Lisp_Charset);
227 #define XCHARSET(x) XRECORD (x, charset, struct Lisp_Charset)
228 #define XSETCHARSET(x, p) XSETRECORD (x, p, charset)
229 #define CHARSETP(x) RECORDP (x, charset)
230 #define GC_CHARSETP(x) GC_RECORDP (x, charset)
231 #define CHECK_CHARSET(x) CHECK_RECORD (x, charset)
232 #define CONCHECK_CHARSET(x) CONCHECK_RECORD (x, charset)
233
234 #define CHARSET_TYPE_94      0 /* This charset includes 94    characters. */
235 #define CHARSET_TYPE_96      1 /* This charset includes 96    characters. */
236 #define CHARSET_TYPE_94X94   2 /* This charset includes 94x94 characters. */
237 #define CHARSET_TYPE_96X96   3 /* This charset includes 96x96 characters. */
238 #define CHARSET_TYPE_128X128 4 /* This charset includes 128x128 characters. */
239 #define CHARSET_TYPE_256X256 5 /* This charset includes 256x256 characters. */
240
241 #define CHARSET_LEFT_TO_RIGHT   0
242 #define CHARSET_RIGHT_TO_LEFT   1
243
244 /* Leading byte and id have been regrouped. -- OG */
245 #define CHARSET_ID(cs)           ((cs)->id)
246 #define CHARSET_LEADING_BYTE(cs) ((Bufbyte)(CHARSET_ID(cs)))
247 #define CHARSET_NAME(cs)         ((cs)->name)
248 #define CHARSET_SHORT_NAME(cs)   ((cs)->short_name)
249 #define CHARSET_LONG_NAME(cs)    ((cs)->long_name)
250 #define CHARSET_COLUMNS(cs)      ((cs)->columns)
251 #define CHARSET_GRAPHIC(cs)      ((cs)->graphic)
252 #define CHARSET_TYPE(cs)         ((cs)->type)
253 #define CHARSET_DIRECTION(cs)    ((cs)->direction)
254 #define CHARSET_FINAL(cs)        ((cs)->final)
255 #define CHARSET_DOC_STRING(cs)   ((cs)->doc_string)
256 #define CHARSET_REGISTRY(cs)     ((cs)->registry)
257 #define CHARSET_CCL_PROGRAM(cs)  ((cs)->ccl_program)
258 #define CHARSET_DIMENSION(cs)    ((cs)->dimension)
259 #define CHARSET_CHARS(cs)        ((cs)->chars)
260 #define CHARSET_REVERSE_DIRECTION_CHARSET(cs) ((cs)->reverse_direction_charset)
261 #define CHARSET_DECODING_TABLE(cs) ((cs)->decoding_table)
262 #define CHARSET_TO_BYTE1_TABLE(cs) ((cs)->to_byte1_table)
263 #define CHARSET_TO_BYTE2_TABLE(cs) ((cs)->to_byte2_table)
264
265
266 #define XCHARSET_ID(cs)           CHARSET_ID           (XCHARSET (cs))
267 #define XCHARSET_NAME(cs)         CHARSET_NAME         (XCHARSET (cs))
268 #define XCHARSET_SHORT_NAME(cs)   CHARSET_SHORT_NAME   (XCHARSET (cs))
269 #define XCHARSET_LONG_NAME(cs)    CHARSET_LONG_NAME    (XCHARSET (cs))
270 #define XCHARSET_GRAPHIC(cs)      CHARSET_GRAPHIC      (XCHARSET (cs))
271 #define XCHARSET_TYPE(cs)         CHARSET_TYPE         (XCHARSET (cs))
272 #define XCHARSET_DIRECTION(cs)    CHARSET_DIRECTION    (XCHARSET (cs))
273 #define XCHARSET_FINAL(cs)        CHARSET_FINAL        (XCHARSET (cs))
274 #define XCHARSET_DOC_STRING(cs)   CHARSET_DOC_STRING   (XCHARSET (cs))
275 #define XCHARSET_REGISTRY(cs)     CHARSET_REGISTRY     (XCHARSET (cs))
276 #define XCHARSET_LEADING_BYTE(cs) CHARSET_LEADING_BYTE (XCHARSET (cs))
277 #define XCHARSET_CCL_PROGRAM(cs)  CHARSET_CCL_PROGRAM  (XCHARSET (cs))
278 #define XCHARSET_DIMENSION(cs)    CHARSET_DIMENSION    (XCHARSET (cs))
279 #define XCHARSET_CHARS(cs)        CHARSET_CHARS        (XCHARSET (cs))
280 #define XCHARSET_REVERSE_DIRECTION_CHARSET(cs) \
281   CHARSET_REVERSE_DIRECTION_CHARSET (XCHARSET (cs))
282 #define XCHARSET_DECODING_TABLE(cs) CHARSET_DECODING_TABLE(XCHARSET(cs))
283 #define XCHARSET_TO_BYTE1_TABLE(cs) CHARSET_TO_BYTE1_TABLE(XCHARSET(cs))
284 #define XCHARSET_TO_BYTE2_TABLE(cs) CHARSET_TO_BYTE2_TABLE(XCHARSET(cs))
285
286 /* Table of charsets indexed by (leading byte - 128). */
287 extern Lisp_Object charset_by_leading_byte[NUM_LEADING_BYTES];
288
289 /* Table of charsets indexed by type/final-byte. */
290 extern Lisp_Object charset_by_attributes[4][128];
291
292 #ifdef ERROR_CHECK_TYPECHECK
293 /* int not Bufbyte even though that is the actual type of a leading byte.
294    This way, out-ot-range values will get caught rather than automatically
295    truncated. */
296 INLINE Lisp_Object CHARSET_BY_LEADING_BYTE (Charset_ID lb);
297 INLINE Lisp_Object
298 CHARSET_BY_LEADING_BYTE (Charset_ID lb)
299 {
300   assert (lb >= MIN_LEADING_BYTE &&
301           lb < (MIN_LEADING_BYTE + NUM_LEADING_BYTES));
302   return charset_by_leading_byte[lb - MIN_LEADING_BYTE];
303 }
304
305 #else
306
307 #define CHARSET_BY_LEADING_BYTE(lb) \
308   (charset_by_leading_byte[(lb) - MIN_LEADING_BYTE])
309
310 #endif
311
312 #define CHARSET_BY_ATTRIBUTES(type, final, dir) \
313   (charset_by_attributes[type][final])
314
315 \f
316 /************************************************************************/
317 /*                        Dealing with characters                       */
318 /************************************************************************/
319
320 #define MAX_CHAR_BASIC_LATIN    0x007F
321
322 #define MIN_CHAR_GREEK          0x0370
323 #define MAX_CHAR_GREEK          0x03CF
324
325 #define MIN_CHAR_CYRILLIC       0x0400
326 #define MAX_CHAR_CYRILLIC       0x045F
327
328 #define MIN_CHAR_HEBREW         0x0590
329 #define MAX_CHAR_HEBREW         0x05EF
330
331 #define MIN_CHAR_THAI           0x0E00
332 #define MAX_CHAR_THAI           0x0E5F
333
334 #define MIN_CHAR_HALFWIDTH_KATAKANA     0xFF60
335 #define MAX_CHAR_HALFWIDTH_KATAKANA     0xFF9F
336
337 #define MIN_CHAR_94             0xE90940
338 #define MAX_CHAR_94             (MIN_CHAR_94 + 94 * 80 - 1)
339 #define MIN_CHAR_96             (MIN_CHAR_94 + 94 * 80)
340 #define MAX_CHAR_96             (MIN_CHAR_96 + 96 * 80 - 1)
341
342 #define MIN_CHAR_94x94          0xE9F6C0
343 #define MAX_CHAR_94x94          (MIN_CHAR_94x94 + 94 * 94 * 80 - 1)
344 #define MIN_CHAR_96x96          0xF4C000
345 #define MAX_CHAR_96x96          (MIN_CHAR_96x96 + 96 * 96 * 80 - 1)
346
347
348 /* Return a character whose charset is CHARSET and position-codes
349    are C1 and C2.  TYPE9N character ignores C2. */
350
351 INLINE Emchar MAKE_CHAR (Lisp_Object charset, int c1, int c2);
352 INLINE Emchar
353 MAKE_CHAR (Lisp_Object charset, int c1, int c2)
354 {
355   Emchar* decoding_table;
356   
357   if ((decoding_table = XCHARSET_DECODING_TABLE (charset)) != NULL)
358     return decoding_table[c1 - (XCHARSET_CHARS (charset) == 94 ? 33 : 32)];
359   else if (EQ (charset, Vcharset_ascii))
360     return c1;
361   else if (EQ (charset, Vcharset_control_1))
362     return c1 | 0x80;
363   else if (EQ (charset, Vcharset_ucs_bmp))
364     return (c1 << 8) | c2;
365   else if (EQ (charset, Vcharset_latin_iso8859_1))
366     return c1 | 0x80;
367   else if (EQ (charset, Vcharset_cyrillic_iso8859_5))
368     return c1 + MIN_CHAR_CYRILLIC - 0x20;
369   else if (EQ (charset, Vcharset_greek_iso8859_7))
370     return c1 + MIN_CHAR_GREEK - 0x20;
371   else if (EQ (charset, Vcharset_hebrew_iso8859_8))
372     return c1 + MIN_CHAR_HEBREW - 0x20;
373   else if (EQ (charset, Vcharset_thai_tis620))
374     return c1 + MIN_CHAR_THAI - 0x20;
375   else if (EQ (charset, Vcharset_katakana_jisx0201))
376     if (c1 < 0x60)
377       return c1 + MIN_CHAR_HALFWIDTH_KATAKANA - 0x20;
378     else
379       /* return MIN_CHAR_94 + ('I' - '0') * 94 + (c1 - 33); */
380       return ' ';
381   else if (XCHARSET_DIMENSION (charset) == 1)
382     {
383       switch (XCHARSET_CHARS (charset))
384         {
385         case 94:
386           return MIN_CHAR_94
387             + (XCHARSET_FINAL (charset) - '0') * 94 + (c1 - 33);
388         case 96:
389           return MIN_CHAR_96
390             + (XCHARSET_FINAL (charset) - '0') * 96 + (c1 - 32);
391         default:
392           abort ();
393         }
394     }
395   else
396     {
397       switch (XCHARSET_CHARS (charset))
398         {
399         case 94:
400           return MIN_CHAR_94x94
401             + (XCHARSET_FINAL (charset) - '0') * 94 * 94
402             + (c1 - 33) * 94 + (c2 - 33);
403         case 96:
404           return MIN_CHAR_96x96
405             + (XCHARSET_FINAL (charset) - '0') * 96 * 96
406             + (c1 - 32) * 96 + (c2 - 32);
407         default:
408           abort ();
409         }
410     }
411 }
412
413 extern Charset_ID    latin_a_char_to_charset[128];
414 extern unsigned char latin_a_char_to_byte1[128];
415 extern unsigned char latin_a_char_to_byte2[128];
416
417 INLINE unsigned char XCHARSET_GET_BYTE1 (Lisp_Object charset, Emchar ch);
418 INLINE unsigned char
419 XCHARSET_GET_BYTE1 (Lisp_Object charset, Emchar ch)
420 {
421   Emchar_to_byte_table* table;
422
423   if ((table = XCHARSET_TO_BYTE1_TABLE (charset)) != NULL)
424     return get_byte_from_character_table (ch, table);
425   else if (EQ (charset, Vcharset_ascii))
426     return ch <= 0x7f ? ch : 0;
427   else if (EQ (charset, Vcharset_control_1))
428     return (0x80 <= ch) && (ch < 0xA0) ? ch & 0x7f : 0;
429   else if (EQ (charset, Vcharset_latin_iso8859_1))
430     return (0xA0 <= ch) && (ch <= 0xff) ? ch & 0x7f : 0;
431   else if (EQ (charset, Vcharset_cyrillic_iso8859_5))
432     return (MIN_CHAR_GREEK <= ch) && (ch <= MAX_CHAR_CYRILLIC) ?
433       ch - MIN_CHAR_CYRILLIC + 0x20 : 0;
434   else if (EQ (charset, Vcharset_greek_iso8859_7))
435     return (MIN_CHAR_GREEK <= ch) && (ch <= MAX_CHAR_GREEK) ?
436       ch - MIN_CHAR_GREEK + 0x20 : 0;
437   else if (EQ (charset, Vcharset_hebrew_iso8859_8))
438     return (MIN_CHAR_HEBREW <= ch) && (ch <= MAX_CHAR_HEBREW) ?
439       ch - MIN_CHAR_HEBREW + 0x20 : 0;
440   else if (EQ (charset, Vcharset_thai_tis620))
441     return (MIN_CHAR_THAI <= ch) && (ch <= MAX_CHAR_THAI) ?
442       ch - MIN_CHAR_THAI + 0x20 : 0;
443   else if (EQ (charset, Vcharset_katakana_jisx0201))
444     return (MIN_CHAR_HALFWIDTH_KATAKANA <= ch)
445       && (ch <= MAX_CHAR_HALFWIDTH_KATAKANA) ?
446       ch - MIN_CHAR_HALFWIDTH_KATAKANA + 0x20 : 0;
447   else if (EQ (charset, Vcharset_ucs_bmp))
448     return ch & 0xff;
449   else if (XCHARSET_DIMENSION (charset) == 1)
450     {
451       if (XCHARSET_CHARS (charset) == 94)
452         return (MIN_CHAR_94 + (XCHARSET_FINAL (charset) - '0') * 94 <= ch)
453           && (ch < MIN_CHAR_94 + (XCHARSET_FINAL (charset) - '0' + 1) * 94) ?
454           ((ch - MIN_CHAR_94) % 94) + 33 : 0;
455       else /* if (XCHARSET_CHARS (charset) == 96) */
456         return (MIN_CHAR_96 + (XCHARSET_FINAL (charset) - '0') * 96 <= ch)
457           && (ch < MIN_CHAR_96 + (XCHARSET_FINAL (charset) - '0' + 1) * 96) ?
458           ((ch - MIN_CHAR_94) % 96) + 32 : 0;
459     }
460   else /* if (XCHARSET_DIMENSION (charset) == 2) */
461     {
462       if (XCHARSET_CHARS (charset) == 94)
463         return (MIN_CHAR_94x94
464                 + (XCHARSET_FINAL (charset) - '0') * 94 * 94 <= ch)
465           && (ch < MIN_CHAR_94x94
466               + (XCHARSET_FINAL (charset) - '0' + 1) * 94 * 94) ?
467           (((ch - MIN_CHAR_94x94) / 94) % 94) + 33 : 0;
468       else /* if (XCHARSET_CHARS (charset) == 96) */
469         return (MIN_CHAR_96x96
470                 + (XCHARSET_FINAL (charset) - '0') * 96 * 96 <= ch)
471           && (ch < MIN_CHAR_96x96
472               + (XCHARSET_FINAL (charset) - '0' + 1) * 96 * 96) ?
473           (((ch - MIN_CHAR_96x96) / 96) % 96) + 32 : 0;
474     }
475 }
476
477 INLINE unsigned char XCHARSET_GET_BYTE2 (Lisp_Object charset, Emchar ch);
478 INLINE unsigned char
479 XCHARSET_GET_BYTE2 (Lisp_Object charset, Emchar ch)
480 {
481   if (XCHARSET_DIMENSION (charset) == 1)
482     return 0;
483   else
484     {
485       Emchar_to_byte_table* table;
486
487       if ((table = XCHARSET_TO_BYTE2_TABLE (charset)) != NULL)
488         return get_byte_from_character_table (ch, table);
489       else if (EQ (charset, Vcharset_ucs_bmp))
490         return (ch >> 8) & 0xff;
491       else if (XCHARSET_CHARS (charset) == 94)
492         return (MIN_CHAR_94x94
493                 + (XCHARSET_FINAL (charset) - '0') * 94 * 94 <= ch)
494           && (ch < MIN_CHAR_94x94
495               + (XCHARSET_FINAL (charset) - '0' + 1) * 94 * 94) ?
496           ((ch - MIN_CHAR_94x94) % 94) + 33 : 0;
497       else /* if (XCHARSET_CHARS (charset) == 96) */
498         return (MIN_CHAR_96x96
499                 + (XCHARSET_FINAL (charset) - '0') * 96 * 96 <= ch)
500           && (ch < MIN_CHAR_96x96
501               + (XCHARSET_FINAL (charset) - '0' + 1) * 96 * 96) ?
502           ((ch - MIN_CHAR_96x96) % 96) + 32 : 0;
503     }
504 }
505
506 INLINE void breakup_char_1 (Emchar c, Lisp_Object *charset, int *c1, int *c2);
507 INLINE void
508 breakup_char_1 (Emchar c, Lisp_Object *charset, int *c1, int *c2)
509 {
510   if (c <= MAX_CHAR_BASIC_LATIN)
511     {
512       *charset = Vcharset_ascii;
513       *c1 = c;
514       *c2 = 0;
515     }
516   else if (c < 0xA0)
517     {
518       *charset = Vcharset_control_1;
519       *c1 = c & 0x7f;
520       *c2 = 0;
521     }
522   else if (c <= 0xff)
523     {
524       *charset = Vcharset_latin_iso8859_1;
525       *c1 = c & 0x7f;
526       *c2 = 0;
527     }
528   else if (c <= 0x17f)
529     {
530       *charset
531         = CHARSET_BY_LEADING_BYTE (latin_a_char_to_charset[c - 0x100]);
532       if (XCHARSET_TO_BYTE1_TABLE (*charset) != NULL)
533         {
534           *c1 = XCHARSET_GET_BYTE1 (*charset, c);
535           *c2 = 0;
536         }
537       else
538         {
539           *c1 = latin_a_char_to_byte1[c - 0x100];
540           *c2 = latin_a_char_to_byte2[c - 0x100];
541         }
542     }
543   else if (c < MIN_CHAR_GREEK)
544     {
545       Lisp_Object charsets = list6 (Vcharset_latin_iso8859_2,
546                                     Vcharset_latin_iso8859_3,
547                                     Vcharset_latin_iso8859_4,
548                                     Vcharset_latin_iso8859_9,
549                                     Vcharset_latin_viscii_lower,
550                                     Vcharset_latin_viscii_upper);
551       while (!EQ (charsets, Qnil))
552         {
553           *charset = Fcar (charsets);
554           if (XCHARSET_TO_BYTE1_TABLE (*charset) != NULL)
555             {
556               if ( (*c1 = XCHARSET_GET_BYTE1 (*charset, c)) )
557                 {
558                   *c2 = 0;
559                   return;
560                 }
561             }
562           charsets = Fcdr (charsets);         
563         }
564       /* otherwise */
565       *charset = Vcharset_ucs_bmp;
566       *c1 = c >> 8;
567       *c2 = c & 0xff;
568     }
569   else if (c <= MAX_CHAR_GREEK)
570     {
571       *charset = Vcharset_greek_iso8859_7;
572       *c1 = c - MIN_CHAR_GREEK + 0x20;
573       *c2 = 0;
574     }
575   else if (c < MIN_CHAR_CYRILLIC)
576     {
577       *charset = Vcharset_ucs_bmp;
578       *c1 = c >> 8;
579       *c2 = c & 0xff;
580     }
581   else if (c <= MAX_CHAR_CYRILLIC)
582     {
583       *charset = Vcharset_cyrillic_iso8859_5;
584       *c1 = c - MIN_CHAR_CYRILLIC + 0x20;
585       *c2 = 0;
586     }
587   else if (c < MIN_CHAR_HEBREW)
588     {
589       *charset = Vcharset_ucs_bmp;
590       *c1 = c >> 8;
591       *c2 = c & 0xff;
592     }
593   else if (c <= MAX_CHAR_HEBREW)
594     {
595       *charset = Vcharset_hebrew_iso8859_8;
596       *c1 = c - MIN_CHAR_HEBREW + 0x20;
597       *c2 = 0;
598     }
599   else if (c < MIN_CHAR_THAI)
600     {
601       *charset = Vcharset_ucs_bmp;
602       *c1 = c >> 8;
603       *c2 = c & 0xff;
604     }
605   else if (c <= MAX_CHAR_THAI)
606     {
607       *charset = Vcharset_thai_tis620;
608       *c1 = c - MIN_CHAR_THAI + 0x20;
609       *c2 = 0;
610     }
611   else if (c < MIN_CHAR_HALFWIDTH_KATAKANA)
612     {
613       Lisp_Object charsets = list3 (Vcharset_latin_jisx0201,
614                                     Vcharset_latin_viscii_lower,
615                                     Vcharset_latin_viscii_upper);
616       while (!EQ (charsets, Qnil))
617         {
618           *charset = Fcar (charsets);
619           if (XCHARSET_TO_BYTE1_TABLE (*charset) != NULL)
620             {
621               if ( (*c1 = XCHARSET_GET_BYTE1 (*charset, c)) )
622                 {
623                   *c2 = 0;
624                   return;
625                 }
626             }
627           charsets = Fcdr (charsets);         
628         }
629       /* otherwise */
630       *charset = Vcharset_ucs_bmp;
631       *c1 = c >> 8;
632       *c2 = c & 0xff;
633     }
634   else if (c <= MAX_CHAR_HALFWIDTH_KATAKANA)
635     {
636       *charset = Vcharset_katakana_jisx0201;
637       *c1 = c - MIN_CHAR_HALFWIDTH_KATAKANA + 0x20;
638       *c2 = 0;
639     }
640   else if (c <= MAX_CHAR_94)
641     {
642       *charset
643         = CHARSET_BY_ATTRIBUTES (CHARSET_TYPE_94,
644                                  ((c - MIN_CHAR_94) / 94) + '0',
645                                  CHARSET_LEFT_TO_RIGHT);
646       *c1 = ((c - MIN_CHAR_94) % 94) + 33;
647       *c2 = 0;
648     }
649   else if (c <= MAX_CHAR_96)
650     {
651       *charset
652         = CHARSET_BY_ATTRIBUTES (CHARSET_TYPE_96,
653                                  ((c - MIN_CHAR_96) / 96) + '0',
654                                  CHARSET_LEFT_TO_RIGHT);
655       *c1 = ((c - MIN_CHAR_96) % 96) + 32;
656       *c2 = 0;
657     }
658   else if (c <= MAX_CHAR_94x94)
659     {
660       *charset
661         = CHARSET_BY_ATTRIBUTES (CHARSET_TYPE_94X94,
662                                  ((c - MIN_CHAR_94x94) / (94 * 94)) + '0',
663                                  CHARSET_LEFT_TO_RIGHT);
664       *c1 = (((c - MIN_CHAR_94x94) / 94) % 94) + 33;
665       *c2 = ((c - MIN_CHAR_94x94) % 94) + 33;
666     }
667   else if (c <= MAX_CHAR_96x96)
668     {
669       *charset
670         = CHARSET_BY_ATTRIBUTES (CHARSET_TYPE_96X96,
671                                  ((c - MIN_CHAR_96x96) / (96 * 96)) + '0',
672                                  CHARSET_LEFT_TO_RIGHT);
673       *c1 = (((c - MIN_CHAR_96x96) / 96) % 96) + 32;
674       *c2 = ((c - MIN_CHAR_96x96) % 96) + 32;
675     }
676   else
677     {
678       printf("u+%x", c);
679       abort();
680     }
681 }
682
683 #define BREAKUP_CHAR(ch, charset, b1, b2) \
684   breakup_char_1 (ch, &(charset), &(b1), &(b2))
685
686 INLINE Lisp_Object CHAR_CHARSET (Emchar ch);
687 INLINE Lisp_Object
688 CHAR_CHARSET (Emchar ch)
689 {
690   Lisp_Object charset;
691   int b1, b2;
692
693   BREAKUP_CHAR(ch, charset, b1, b2);
694   return charset;
695 }
696
697 #define CHAR_LEADING_BYTE(c) (XCHARSET_LEADING_BYTE(CHAR_CHARSET(c)))
698
699 #define CHAR_COLUMNS(c)     (CHARSET_COLUMNS(XCHARSET(CHAR_CHARSET(c))))
700
701 \f
702 /************************************************************************/
703 /*                            Exported functions                        */
704 /************************************************************************/
705
706 EXFUN (Ffind_charset, 1);
707 EXFUN (Fget_charset, 1);
708
709 extern Lisp_Object Vcharset_chinese_big5_1;
710 extern Lisp_Object Vcharset_chinese_big5_2;
711 extern Lisp_Object Vcharset_japanese_jisx0208;
712 extern Lisp_Object Vcharset_japanese_jisx0212;
713
714 #endif /* _XEMACS_CHAR_UCS_H */