db8df9247b4d9f2f62b8ecf7dd327742c6a475e6
[chise/xemacs-chise.git-] / src / character.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_CHARACTER_H
25 #define _XEMACS_CHARACTER_H
26
27 #ifndef CHAR_IS_UCS4
28 #include "mule-charset.h"
29 #else
30
31 /************************************************************************/
32 /*                    Definition of leading bytes                       */
33 /************************************************************************/
34
35 #define MIN_LEADING_BYTE                0x80
36 /* These need special treatment in a string and/or character */
37 #define LEADING_BYTE_ASCII              0x8E /* Omitted in a buffer */
38 #define LEADING_BYTE_CONTROL_1          0x8F /* represent normal 80-9F */
39
40 /** The following are for 1-byte characters in an official charset. **/
41
42 #define LEADING_BYTE_LATIN_ISO8859_1    0x81 /* Right half of ISO 8859-1 */
43 #define LEADING_BYTE_LATIN_ISO8859_2    0x82 /* Right half of ISO 8859-2 */
44 #define LEADING_BYTE_LATIN_ISO8859_3    0x83 /* Right half of ISO 8859-3 */
45 #define LEADING_BYTE_LATIN_ISO8859_4    0x84 /* Right half of ISO 8859-4 */
46 #define LEADING_BYTE_THAI_TIS620        0x85 /* TIS620-2533 */
47 #define LEADING_BYTE_GREEK_ISO8859_7    0x86 /* Right half of ISO 8859-7 */
48 #define LEADING_BYTE_ARABIC_ISO8859_6   0x87 /* Right half of ISO 8859-6 */
49 #define LEADING_BYTE_HEBREW_ISO8859_8   0x88 /* Right half of ISO 8859-8 */
50 #define LEADING_BYTE_KATAKANA_JISX0201  0x89 /* Right half of JIS X0201-1976 */
51 #define LEADING_BYTE_LATIN_JISX0201     0x8A /* Left  half of JIS X0201-1976 */
52 #define LEADING_BYTE_CYRILLIC_ISO8859_5 0x8C /* Right half of ISO 8859-5 */
53 #define LEADING_BYTE_LATIN_ISO8859_9    0x8D /* Right half of ISO 8859-9 */
54
55 #define MIN_LEADING_BYTE_OFFICIAL_1     LEADING_BYTE_LATIN_ISO8859_1
56 #define MAX_LEADING_BYTE_OFFICIAL_1     LEADING_BYTE_LATIN_ISO8859_9
57
58 #define LEADING_BYTE_CHINESE_BIG5_1     0xB0    /* Big5 Level 1 */
59 #define LEADING_BYTE_CHINESE_BIG5_2     0xB1    /* Big5 Level 2 */
60 #define MIN_LEADING_BYTE_PRIVATE_2      0xB0
61 #define MAX_LEADING_BYTE_PRIVATE_2      0xBF
62
63 /** The following are for 2-byte characters in an official charset. **/
64
65 #define LEADING_BYTE_JAPANESE_JISX0208_1978 0xC0/* Japanese JIS X0208-1978 */
66 #define LEADING_BYTE_CHINESE_GB2312     0xC1    /* Chinese Hanzi GB2312-1980 */
67 #define LEADING_BYTE_JAPANESE_JISX0208  0xC2    /* Japanese JIS X0208-1983 */
68 #define LEADING_BYTE_KOREAN_KSC5601     0xC3    /* Hangul KS C5601-1987 */
69 #define LEADING_BYTE_JAPANESE_JISX0212  0xC4    /* Japanese JIS X0212-1990 */
70 #define LEADING_BYTE_CHINESE_CCITT_GB   0xC5    /* CCITT Extended GB */
71 #define LEADING_BYTE_CHINESE_CNS11643_1 0xC7    /* Chinese CNS11643 Set 1 */
72 #define LEADING_BYTE_CHINESE_CNS11643_2 0xC8    /* Chinese CNS11643 Set 2 */
73 #define LEADING_BYTE_CHINESE_CNS11643_3 0xC9    /* Chinese CNS11643 Set 3 */
74 #define LEADING_BYTE_CHINESE_CNS11643_4 0xCA    /* Chinese CNS11643 Set 4 */
75 #define LEADING_BYTE_CHINESE_CNS11643_5 0xCB    /* Chinese CNS11643 Set 5 */
76 #define LEADING_BYTE_CHINESE_CNS11643_6 0xCC    /* Chinese CNS11643 Set 6 */
77 #define LEADING_BYTE_CHINESE_CNS11643_7 0xCD    /* Chinese CNS11643 Set 7 */
78 #define LEADING_BYTE_KOREAN_KPS9566     0xCE    /* DPRK Hangul KPS 9566-1997 */
79
80 #define MIN_LEADING_BYTE_OFFICIAL_2     LEADING_BYTE_JAPANESE_JISX0208_1978
81 #define MAX_LEADING_BYTE_OFFICIAL_2     LEADING_BYTE_KOREAN_KPS9566
82
83 /** The following are for 1- and 2-byte characters in a private charset. **/
84
85 #define PRE_LEADING_BYTE_PRIVATE_1      0x120   /* 1-byte char-set */
86 #define PRE_LEADING_BYTE_PRIVATE_2      0x121   /* 2-byte char-set */
87
88 #define MIN_LEADING_BYTE_PRIVATE_1      0x0D0
89 #define MAX_LEADING_BYTE_PRIVATE_1      0x11f
90
91 #define NUM_LEADING_BYTES 256
92
93 \f
94 /************************************************************************/
95 /*                     Operations on individual bytes                   */
96 /*                             of any format                            */
97 /************************************************************************/
98
99 /* Argument `c' should be (unsigned int) or (unsigned char). */
100 /* Note that SP and DEL are not included. */
101
102 #define BYTE_ASCII_P(c) ((c) < 0x80)
103 #define BYTE_C0_P(c) ((c) < 0x20)
104 /* Do some forced casting just to make *sure* things are gotten right. */
105 #define BYTE_C1_P(c) ((unsigned int) ((unsigned int) (c) - 0x80) < 0x20)
106
107 \f
108 /************************************************************************/
109 /*                     Operations on individual bytes                   */
110 /*                       in a Mule-formatted string                     */
111 /************************************************************************/
112
113 /* Does this byte represent the first byte of a character? */
114
115 INLINE int
116 BUFBYTE_FIRST_BYTE_P(Bufbyte c)
117 {
118   return (c <= 0x7f) || (0xc0 <= c);
119 }
120
121 \f
122 /************************************************************************/
123 /*            Information about a particular character set              */
124 /************************************************************************/
125
126 struct Lisp_Charset
127 {
128   struct lcrecord_header header;
129
130   int id;
131   Lisp_Object name;
132   Lisp_Object doc_string, registry, short_name, long_name;
133
134   Lisp_Object reverse_direction_charset;
135
136   Lisp_Object ccl_program;
137
138   /* Final byte of this character set in ISO2022 designating escape sequence */
139   Bufbyte final;
140
141   /* Number of bytes (1 - 4) required in the internal representation
142      for characters in this character set.  This is *not* the
143      same as the dimension of the character set). */
144   unsigned int rep_bytes;
145
146   /* Number of columns a character in this charset takes up, on TTY
147      devices.  Not used for X devices. */
148   unsigned int columns;
149
150   /* Direction of this character set */
151   unsigned int direction;
152
153   /* Type of this character set (94, 96, 94x94, 96x96) */
154   unsigned int type;
155
156   /* Number of bytes used in encoding of this character set (1 or 2) */
157   unsigned int dimension;
158
159   /* Number of chars in each dimension (usually 94 or 96) */
160   unsigned int chars;
161
162   /* Which half of font to be used to display this character set */
163   unsigned int graphic;
164 };
165
166 DECLARE_LRECORD (charset, struct Lisp_Charset);
167 #define XCHARSET(x) XRECORD (x, charset, struct Lisp_Charset)
168 #define XSETCHARSET(x, p) XSETRECORD (x, p, charset)
169 #define CHARSETP(x) RECORDP (x, charset)
170 #define GC_CHARSETP(x) GC_RECORDP (x, charset)
171 #define CHECK_CHARSET(x) CHECK_RECORD (x, charset)
172 #define CONCHECK_CHARSET(x) CONCHECK_RECORD (x, charset)
173
174 #define CHARSET_TYPE_94    0    /* This charset includes 94    characters. */
175 #define CHARSET_TYPE_96    1    /* This charset includes 96    characters. */
176 #define CHARSET_TYPE_94X94 2    /* This charset includes 94x94 characters. */
177 #define CHARSET_TYPE_96X96 3    /* This charset includes 96x96 characters. */
178
179 #define CHARSET_LEFT_TO_RIGHT   0
180 #define CHARSET_RIGHT_TO_LEFT   1
181
182 /* Leading byte and id have been regrouped. -- OG */
183 #define CHARSET_ID(cs)           ((cs)->id)
184 #define CHARSET_LEADING_BYTE(cs) ((Bufbyte)(CHARSET_ID(cs)))
185 #define CHARSET_NAME(cs)         ((cs)->name)
186 #define CHARSET_SHORT_NAME(cs)   ((cs)->short_name)
187 #define CHARSET_LONG_NAME(cs)    ((cs)->long_name)
188 #define CHARSET_REP_BYTES(cs)    ((cs)->rep_bytes)
189 #define CHARSET_COLUMNS(cs)      ((cs)->columns)
190 #define CHARSET_GRAPHIC(cs)      ((cs)->graphic)
191 #define CHARSET_TYPE(cs)         ((cs)->type)
192 #define CHARSET_DIRECTION(cs)    ((cs)->direction)
193 #define CHARSET_FINAL(cs)        ((cs)->final)
194 #define CHARSET_DOC_STRING(cs)   ((cs)->doc_string)
195 #define CHARSET_REGISTRY(cs)     ((cs)->registry)
196 #define CHARSET_CCL_PROGRAM(cs)  ((cs)->ccl_program)
197 #define CHARSET_DIMENSION(cs)    ((cs)->dimension)
198 #define CHARSET_CHARS(cs)        ((cs)->chars)
199 #define CHARSET_REVERSE_DIRECTION_CHARSET(cs) ((cs)->reverse_direction_charset)
200
201
202 #define XCHARSET_ID(cs)           CHARSET_ID           (XCHARSET (cs))
203 #define XCHARSET_NAME(cs)         CHARSET_NAME         (XCHARSET (cs))
204 #define XCHARSET_SHORT_NAME(cs)   CHARSET_SHORT_NAME   (XCHARSET (cs))
205 #define XCHARSET_LONG_NAME(cs)    CHARSET_LONG_NAME    (XCHARSET (cs))
206 #define XCHARSET_REP_BYTES(cs)    CHARSET_REP_BYTES    (XCHARSET (cs))
207 #define XCHARSET_COLUMNS(cs)      CHARSET_COLUMNS      (XCHARSET (cs))
208 #define XCHARSET_GRAPHIC(cs)      CHARSET_GRAPHIC      (XCHARSET (cs))
209 #define XCHARSET_TYPE(cs)         CHARSET_TYPE         (XCHARSET (cs))
210 #define XCHARSET_DIRECTION(cs)    CHARSET_DIRECTION    (XCHARSET (cs))
211 #define XCHARSET_FINAL(cs)        CHARSET_FINAL        (XCHARSET (cs))
212 #define XCHARSET_DOC_STRING(cs)   CHARSET_DOC_STRING   (XCHARSET (cs))
213 #define XCHARSET_REGISTRY(cs)     CHARSET_REGISTRY     (XCHARSET (cs))
214 #define XCHARSET_LEADING_BYTE(cs) CHARSET_LEADING_BYTE (XCHARSET (cs))
215 #define XCHARSET_CCL_PROGRAM(cs)  CHARSET_CCL_PROGRAM  (XCHARSET (cs))
216 #define XCHARSET_DIMENSION(cs)    CHARSET_DIMENSION    (XCHARSET (cs))
217 #define XCHARSET_CHARS(cs)        CHARSET_CHARS        (XCHARSET (cs))
218 #ifdef CHARSET_PRIVATE_P
219 #define XCHARSET_PRIVATE_P(cs)    CHARSET_PRIVATE_P    (XCHARSET (cs))
220 #endif
221 #define XCHARSET_REVERSE_DIRECTION_CHARSET(cs) \
222   CHARSET_REVERSE_DIRECTION_CHARSET (XCHARSET (cs))
223
224 /* Table of charsets indexed by (leading byte - 128). */
225 extern Lisp_Object charset_by_leading_byte[NUM_LEADING_BYTES];
226
227 /* Table of charsets indexed by type/final-byte/direction. */
228 extern Lisp_Object charset_by_attributes[4][128][2];
229
230 #ifdef ERROR_CHECK_TYPECHECK
231 /* int not Bufbyte even though that is the actual type of a leading byte.
232    This way, out-ot-range values will get caught rather than automatically
233    truncated. */
234 INLINE Lisp_Object CHARSET_BY_LEADING_BYTE (int lb);
235 INLINE Lisp_Object
236 CHARSET_BY_LEADING_BYTE (int lb)
237 {
238   assert (lb >= MIN_LEADING_BYTE &&
239           lb < (MIN_LEADING_BYTE + NUM_LEADING_BYTES));
240   return charset_by_leading_byte[lb - MIN_LEADING_BYTE];
241 }
242
243 #else
244
245 #define CHARSET_BY_LEADING_BYTE(lb) \
246   (charset_by_leading_byte[(lb) - MIN_LEADING_BYTE])
247
248 #endif
249
250 #define CHARSET_BY_ATTRIBUTES(type, final, dir) \
251   (charset_by_attributes[type][final][dir])
252
253 INLINE int REP_BYTES_BY_FIRST_BYTE (int fb);
254 INLINE int
255 REP_BYTES_BY_FIRST_BYTE (int fb)
256 {
257   if ( fb < 0xc0 )
258     return 1;
259   else if ( fb < 0xe0 )
260     return 2;
261   else if ( fb < 0xf0 )
262     return 3;
263   else if ( fb < 0xf8 )
264     return 4;
265   else if ( fb < 0xfc )
266     return 5;
267   else
268     return 6;
269 }
270
271 \f
272 /************************************************************************/
273 /*                        Dealing with characters                       */
274 /************************************************************************/
275
276 /* Is this character represented by more than one byte in a string? */
277
278 #define CHAR_MULTIBYTE_P(c) ((c) >= 0x80)
279
280 #define CHAR_ASCII_P(c) (!CHAR_MULTIBYTE_P (c))
281
282 /* The bit fields of character are divided into 3 parts:
283    FIELD1(5bits):FIELD2(7bits):FIELD3(7bits) */
284
285 #define CHAR_FIELD1_MASK (0x7F << 14)
286 #define CHAR_FIELD2_MASK (0x7F << 7)
287 #define CHAR_FIELD3_MASK 0x7F
288
289 #define MIN_CHAR_GREEK          0x0370
290 #define MAX_CHAR_GREEK          0x03CF
291
292 #define MIN_CHAR_CYRILLIC       0x0400
293 #define MAX_CHAR_CYRILLIC       0x045F
294
295 #define MIN_CHAR_HEBREW         0x0590
296 #define MAX_CHAR_HEBREW         0x05EF
297
298 #define MIN_CHAR_THAI           0x0E00
299 #define MAX_CHAR_THAI           0x0E5F
300
301 #define MIN_CHAR_HALFWIDTH_KATAKANA     0xFF60
302 #define MAX_CHAR_HALFWIDTH_KATAKANA     0xFF9F
303
304 /* Macros to access each field of a character code of C.  */
305
306 #define CHAR_FIELD1(c) (((c) & CHAR_FIELD1_MASK) >> 14)
307 #define CHAR_FIELD2_INTERNAL(c) (((c) & CHAR_FIELD2_MASK) >> 7)
308 #define CHAR_FIELD3_INTERNAL(c)  ((c) & CHAR_FIELD3_MASK)
309
310 /* Field 1, if non-zero, usually holds a leading byte for a
311    dimension-2 charset.  Field 2, if non-zero, usually holds a leading
312    byte for a dimension-1 charset. */
313
314 /* Converting between field values and leading bytes.  */
315
316 #define FIELD1_TO_PRIVATE_LEADING_BYTE  0x80
317 #define FIELD1_TO_OFFICIAL_LEADING_BYTE 0x80
318 #define FIELD2_TO_PRIVATE_LEADING_BYTE  0xb0
319 #define FIELD2_TO_OFFICIAL_LEADING_BYTE 0x80
320
321 /* Minimum and maximum allowed values for the fields. */
322
323 #define MIN_CHAR_FIELD2_OFFICIAL \
324   (MIN_LEADING_BYTE_OFFICIAL_1 - FIELD2_TO_OFFICIAL_LEADING_BYTE)
325 #define MAX_CHAR_FIELD2_OFFICIAL \
326   (MAX_LEADING_BYTE_OFFICIAL_1 - FIELD2_TO_OFFICIAL_LEADING_BYTE)
327
328 #define MIN_CHAR_FIELD1_OFFICIAL \
329   (MIN_LEADING_BYTE_OFFICIAL_2 - FIELD1_TO_OFFICIAL_LEADING_BYTE)
330 #define MAX_CHAR_FIELD1_OFFICIAL \
331   (MAX_LEADING_BYTE_OFFICIAL_2 - FIELD1_TO_OFFICIAL_LEADING_BYTE)
332
333 #define MIN_CHAR_FIELD2_PRIVATE \
334   (MIN_LEADING_BYTE_PRIVATE_1 - FIELD2_TO_PRIVATE_LEADING_BYTE)
335 #define MAX_CHAR_FIELD2_PRIVATE \
336   (MAX_LEADING_BYTE_PRIVATE_1 - FIELD2_TO_PRIVATE_LEADING_BYTE)
337
338 #define MIN_CHAR_FIELD1_PRIVATE \
339   (MIN_LEADING_BYTE_PRIVATE_2 - FIELD1_TO_PRIVATE_LEADING_BYTE)
340 #define MAX_CHAR_FIELD1_PRIVATE \
341   (MAX_LEADING_BYTE_PRIVATE_2 - FIELD1_TO_PRIVATE_LEADING_BYTE)
342
343 /* Minimum character code of each <type> character.  */
344
345 #define MULE_CHAR_PRIVATE_OFFSET (0xe0 << 16)
346
347 #define MIN_CHAR_OFFICIAL_TYPE9N \
348   (MULE_CHAR_PRIVATE_OFFSET | (MIN_CHAR_FIELD2_OFFICIAL <<  7))
349 #define MIN_CHAR_PRIVATE_TYPE9N \
350   (MULE_CHAR_PRIVATE_OFFSET | (MIN_CHAR_FIELD2_PRIVATE  <<  7))
351 #define MIN_CHAR_PRIVATE_TYPE9NX9N \
352   (MULE_CHAR_PRIVATE_OFFSET | (MIN_CHAR_FIELD1_PRIVATE  << 14))
353 #define MIN_CHAR_OFFICIAL_TYPE9NX9N \
354   (MULE_CHAR_PRIVATE_OFFSET | (MIN_CHAR_FIELD1_OFFICIAL << 14))
355 #define MIN_CHAR_COMPOSITION \
356   (MULE_CHAR_PRIVATE_OFFSET | (0x7f << 14))
357
358 /* Return a character whose charset is CHARSET and position-codes
359    are C1 and C2.  TYPE9N character ignores C2.
360
361    NOTE: This takes advantage of the fact that
362    FIELD2_TO_OFFICIAL_LEADING_BYTE and
363    FIELD2_TO_PRIVATE_LEADING_BYTE are the same.
364    */
365
366 INLINE Emchar MAKE_CHAR (Lisp_Object charset, int c1, int c2);
367 INLINE Emchar
368 MAKE_CHAR (Lisp_Object charset, int c1, int c2)
369 {
370   if (EQ (charset, Vcharset_ascii))
371     return c1;
372   else if (EQ (charset, Vcharset_control_1))
373     return c1 | 0x80;
374   else if (EQ (charset, Vcharset_latin_iso8859_1))
375     return c1 | 0x80;
376   else if (EQ (charset, Vcharset_greek_iso8859_7))
377     return c1 + MIN_CHAR_GREEK - 0x20;
378   else if (EQ (charset, Vcharset_cyrillic_iso8859_5))
379     return c1 + MIN_CHAR_CYRILLIC - 0x20;
380   else if (EQ (charset, Vcharset_hebrew_iso8859_8))
381     return c1 + MIN_CHAR_HEBREW - 0x20;
382   else if (EQ (charset, Vcharset_thai_tis620))
383     return c1 + MIN_CHAR_THAI - 0x20;
384   else if (EQ (charset, Vcharset_katakana_jisx0201))
385     if (c1 < 0x60)
386       return c1 + MIN_CHAR_HALFWIDTH_KATAKANA - 0x20;
387     else
388       return 32;
389 #ifdef ENABLE_COMPOSITE_CHARS
390   else if (EQ (charset, Vcharset_composite))
391     return (0x1F << 14) | ((c1) << 7) | (c2);
392 #endif
393   else if (XCHARSET_DIMENSION (charset) == 1)
394     return MULE_CHAR_PRIVATE_OFFSET
395       | ((XCHARSET_LEADING_BYTE (charset) -
396           FIELD2_TO_OFFICIAL_LEADING_BYTE) << 7) | (c1);
397   else
398     return MULE_CHAR_PRIVATE_OFFSET
399       | ((XCHARSET_LEADING_BYTE (charset) -
400           FIELD1_TO_PRIVATE_LEADING_BYTE) << 14) | ((c1) << 7) | (c2);
401 }
402
403 INLINE void breakup_char_1 (Emchar c, Lisp_Object *charset, int *c1, int *c2);
404 INLINE void
405 breakup_char_1 (Emchar c, Lisp_Object *charset, int *c1, int *c2)
406 {
407   if (CHAR_ASCII_P (c))
408     {
409       *charset = Vcharset_ascii;
410       *c1 = c;
411       *c2 = 0;
412     }
413   else if (c < 0xA0)
414     {
415       *charset = Vcharset_control_1;
416       *c1 = CHAR_FIELD3_INTERNAL (c);
417       *c2 = 0;
418     }
419   else if (c <= 0xff)
420     {
421       *charset = Vcharset_latin_iso8859_1;
422       *c1 = CHAR_FIELD3_INTERNAL (c);
423       *c2 = 0;
424     }
425   else if (c < MIN_CHAR_GREEK)
426     {
427       abort ();
428     }
429   else if (c <= MAX_CHAR_GREEK)
430     {
431       *charset = Vcharset_greek_iso8859_7;
432       *c1 = c - MIN_CHAR_GREEK + 0x20;
433       *c2 = 0;
434     }
435   else if (c < MIN_CHAR_CYRILLIC)
436     {
437       abort ();
438     }
439   else if (c <= MAX_CHAR_CYRILLIC)
440     {
441       *charset = Vcharset_cyrillic_iso8859_5;
442       *c1 = c - MIN_CHAR_CYRILLIC + 0x20;
443       *c2 = 0;
444     }
445   else if (c < MIN_CHAR_HEBREW)
446     {
447       abort ();
448     }
449   else if (c <= MAX_CHAR_HEBREW)
450     {
451       *charset = Vcharset_hebrew_iso8859_8;
452       *c1 = c - MIN_CHAR_HEBREW + 0x20;
453       *c2 = 0;
454     }
455   else if (c < MIN_CHAR_THAI)
456     {
457       abort ();
458     }
459   else if (c <= MAX_CHAR_THAI)
460     {
461       *charset = Vcharset_thai_tis620;
462       *c1 = c - MIN_CHAR_THAI + 0x20;
463       *c2 = 0;
464     }
465   else if (c < MIN_CHAR_HALFWIDTH_KATAKANA)
466     {
467       abort ();
468     }
469   else if (c <= MAX_CHAR_HALFWIDTH_KATAKANA)
470     {
471       *charset = Vcharset_katakana_jisx0201;
472       *c1 = c - MIN_CHAR_HALFWIDTH_KATAKANA + 0x20;
473       *c2 = 0;
474     }
475   else if (c < MIN_CHAR_PRIVATE_TYPE9NX9N)
476     {
477       *charset
478         = CHARSET_BY_LEADING_BYTE (CHAR_FIELD2_INTERNAL (c) +
479                                    FIELD2_TO_OFFICIAL_LEADING_BYTE);
480       *c1 = CHAR_FIELD3_INTERNAL(c);
481       *c2 = 0;
482     }
483   else if (c < MIN_CHAR_COMPOSITION)
484     {
485       *charset
486         = CHARSET_BY_LEADING_BYTE (CHAR_FIELD1 (c) +
487                                    FIELD1_TO_OFFICIAL_LEADING_BYTE);
488       *c1 = CHAR_FIELD2_INTERNAL(c);
489       *c2 = CHAR_FIELD3_INTERNAL(c);
490     }
491   else
492     {
493 #ifdef ENABLE_COMPOSITE_CHARS
494       return LEADING_BYTE_COMPOSITE;
495 #else
496       printf("u+%x", c);
497       abort();
498 #endif /* ENABLE_COMPOSITE_CHARS */
499     }
500 }
501
502 #define BREAKUP_CHAR(c, charset, c1, c2) \
503   breakup_char_1 (c, &(charset), &(c1), &(c2))
504
505 INLINE Lisp_Object CHAR_CHARSET (Emchar c);
506 INLINE Lisp_Object
507 CHAR_CHARSET (Emchar c)
508 {
509   Lisp_Object charset;
510   int c1, c2;
511
512   BREAKUP_CHAR(c, charset, c1, c2);
513   return charset;
514 }
515
516 #define CHAR_LEADING_BYTE(c) (XCHARSET_LEADING_BYTE(CHAR_CHARSET(c)))
517
518 \f
519 #ifdef ENABLE_COMPOSITE_CHARS
520 /************************************************************************/
521 /*                           Composite characters                       */
522 /************************************************************************/
523
524 Emchar lookup_composite_char (Bufbyte *str, int len);
525 Lisp_Object composite_char_string (Emchar ch);
526 #endif /* ENABLE_COMPOSITE_CHARS */
527
528 \f
529 /************************************************************************/
530 /*                            Exported functions                        */
531 /************************************************************************/
532
533 EXFUN (Ffind_charset, 1);
534 EXFUN (Fget_charset, 1);
535
536 extern Lisp_Object Vcharset_chinese_big5_1;
537 extern Lisp_Object Vcharset_chinese_big5_2;
538 extern Lisp_Object Vcharset_japanese_jisx0208;
539
540 Emchar Lstream_get_emchar_1 (Lstream *stream, int first_char);
541 int Lstream_fput_emchar (Lstream *stream, Emchar ch);
542 void Lstream_funget_emchar (Lstream *stream, Emchar ch);
543
544 int copy_internal_to_external (CONST Bufbyte *internal, Bytecount len,
545                                unsigned char *external);
546 Bytecount copy_external_to_internal (CONST unsigned char *external,
547                                      int len, Bufbyte *internal);
548
549 #endif /* CHAR_IS_UCS4 */
550 #endif /* _XEMACS_CHARACTER_H */