1 /* Header for UCS-4 character representation.
2 Copyright (C) 1999 Electrotechnical Laboratory, JAPAN.
3 Licensed to the Free Software Foundation.
5 This file is part of XEmacs.
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
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
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. */
22 /* Rewritten by MORIOKA Tomohiko <tomo@m17n.org>. */
24 #ifndef _XEMACS_CHARACTER_H
25 #define _XEMACS_CHARACTER_H
28 #include "mule-charset.h"
31 /************************************************************************/
32 /* Definition of leading bytes */
33 /************************************************************************/
35 typedef int Charset_ID;
37 #define MIN_LEADING_BYTE 0x80
39 #define LEADING_BYTE_UCS_BMP 0x80
40 #define LEADING_BYTE_CONTROL_1 0x81 /* represent normal 80-9F */
43 #define CHARSET_ID_OFFSET_94 0x60
45 #define MIN_CHARSET_ID_PRIVATE_94 (CHARSET_ID_OFFSET_94 + '0')
46 #define MAX_CHARSET_ID_PRIVATE_94 (CHARSET_ID_OFFSET_94 + '?')
49 #define LEADING_BYTE_ASCII (CHARSET_ID_OFFSET_94 + 'B')
51 /* Right half of JIS X0201-1976 */
52 #define LEADING_BYTE_KATAKANA_JISX0201 (CHARSET_ID_OFFSET_94 + 'I')
54 /* Left half of JIS X0201-1976 */
55 #define LEADING_BYTE_LATIN_JISX0201 (CHARSET_ID_OFFSET_94 + 'J')
58 #define CHARSET_ID_OFFSET_96 0x70
60 /* Right half of ISO 8859-1 */
61 #define LEADING_BYTE_LATIN_ISO8859_1 (CHARSET_ID_OFFSET_96 + 'A')
63 /* Right half of ISO 8859-2 */
64 #define LEADING_BYTE_LATIN_ISO8859_2 (CHARSET_ID_OFFSET_96 + 'B')
66 /* Right half of ISO 8859-3 */
67 #define LEADING_BYTE_LATIN_ISO8859_3 (CHARSET_ID_OFFSET_96 + 'C')
69 /* Right half of ISO 8859-4 */
70 #define LEADING_BYTE_LATIN_ISO8859_4 (CHARSET_ID_OFFSET_96 + 'D')
72 /* Right half of ISO 8859-7 */
73 #define LEADING_BYTE_GREEK_ISO8859_7 (CHARSET_ID_OFFSET_96 + 'F')
75 /* Right half of ISO 8859-6 */
76 #define LEADING_BYTE_ARABIC_ISO8859_6 (CHARSET_ID_OFFSET_96 + 'G')
78 /* Right half of ISO 8859-8 */
79 #define LEADING_BYTE_HEBREW_ISO8859_8 (CHARSET_ID_OFFSET_96 + 'H')
81 /* Right half of ISO 8859-5 */
82 #define LEADING_BYTE_CYRILLIC_ISO8859_5 (CHARSET_ID_OFFSET_96 + 'L')
84 /* Right half of ISO 8859-9 */
85 #define LEADING_BYTE_LATIN_ISO8859_9 (CHARSET_ID_OFFSET_96 + 'M')
88 #define LEADING_BYTE_THAI_TIS620 (CHARSET_ID_OFFSET_96 + 'T')
91 #define MIN_LEADING_BYTE_PRIVATE_1 0xD0
92 #define MAX_LEADING_BYTE_PRIVATE_1 0xDF
95 #define CHARSET_ID_OFFSET_94x94 0xB0
98 #define LEADING_BYTE_CHINESE_BIG5_1 ('0' + CHARSET_ID_OFFSET_94x94)
101 #define LEADING_BYTE_CHINESE_BIG5_2 ('1' + CHARSET_ID_OFFSET_94x94)
103 #define MIN_LEADING_BYTE_PRIVATE_2 ('0' + CHARSET_ID_OFFSET_94x94)
104 #define MAX_LEADING_BYTE_PRIVATE_2 ('?' + CHARSET_ID_OFFSET_94x94)
106 /* Japanese JIS X0208-1978 */
107 #define LEADING_BYTE_JAPANESE_JISX0208_1978 \
108 ('@' + CHARSET_ID_OFFSET_94x94)
110 /* Chinese Hanzi GB2312-1980 */
111 #define LEADING_BYTE_CHINESE_GB2312 ('A' + CHARSET_ID_OFFSET_94x94)
113 /* Japanese JIS X0208-1983 */
114 #define LEADING_BYTE_JAPANESE_JISX0208 ('B' + CHARSET_ID_OFFSET_94x94)
116 /* Hangul KS C5601-1987 */
117 #define LEADING_BYTE_KOREAN_KSC5601 ('C' + CHARSET_ID_OFFSET_94x94)
119 /* Japanese JIS X0212-1990 */
120 #define LEADING_BYTE_JAPANESE_JISX0212 ('D' + CHARSET_ID_OFFSET_94x94)
122 /* CCITT Extended GB */
123 #define LEADING_BYTE_CHINESE_CCITT_GB ('E' + CHARSET_ID_OFFSET_94x94)
125 /* Chinese CNS11643 Set 1 */
126 #define LEADING_BYTE_CHINESE_CNS11643_1 ('G' + CHARSET_ID_OFFSET_94x94)
128 /* Chinese CNS11643 Set 2 */
129 #define LEADING_BYTE_CHINESE_CNS11643_2 ('H' + CHARSET_ID_OFFSET_94x94)
131 /* Chinese CNS11643 Set 3 */
132 #define LEADING_BYTE_CHINESE_CNS11643_3 ('I' + CHARSET_ID_OFFSET_94x94)
134 /* Chinese CNS11643 Set 4 */
135 #define LEADING_BYTE_CHINESE_CNS11643_4 ('J' + CHARSET_ID_OFFSET_94x94)
137 /* Chinese CNS11643 Set 5 */
138 #define LEADING_BYTE_CHINESE_CNS11643_5 ('K' + CHARSET_ID_OFFSET_94x94)
140 /* Chinese CNS11643 Set 6 */
141 #define LEADING_BYTE_CHINESE_CNS11643_6 ('L' + CHARSET_ID_OFFSET_94x94)
143 /* Chinese CNS11643 Set 7 */
144 #define LEADING_BYTE_CHINESE_CNS11643_7 ('M' + CHARSET_ID_OFFSET_94x94)
146 /* DPRK Hangul KPS 9566-1997 */
147 #define LEADING_BYTE_KOREAN_KPS9566 ('N' + CHARSET_ID_OFFSET_94x94)
149 #define MIN_LEADING_BYTE_OFFICIAL_2 LEADING_BYTE_JAPANESE_JISX0208_1978
150 #define MAX_LEADING_BYTE_OFFICIAL_2 LEADING_BYTE_KOREAN_KPS9566
152 #define NUM_LEADING_BYTES 256
155 /************************************************************************/
156 /* Operations on individual bytes */
158 /************************************************************************/
160 /* Argument `c' should be (unsigned int) or (unsigned char). */
161 /* Note that SP and DEL are not included. */
163 #define BYTE_ASCII_P(c) ((c) < 0x80)
164 #define BYTE_C0_P(c) ((c) < 0x20)
165 /* Do some forced casting just to make *sure* things are gotten right. */
166 #define BYTE_C1_P(c) ((unsigned int) ((unsigned int) (c) - 0x80) < 0x20)
169 /************************************************************************/
170 /* Information about a particular character set */
171 /************************************************************************/
175 struct lcrecord_header header;
179 Lisp_Object doc_string, registry, short_name, long_name;
181 Lisp_Object reverse_direction_charset;
183 Lisp_Object ccl_program;
185 /* Final byte of this character set in ISO2022 designating escape sequence */
188 /* Number of bytes (1 - 4) required in the internal representation
189 for characters in this character set. This is *not* the
190 same as the dimension of the character set). */
191 unsigned int rep_bytes;
193 /* Number of columns a character in this charset takes up, on TTY
194 devices. Not used for X devices. */
195 unsigned int columns;
197 /* Direction of this character set */
198 unsigned int direction;
200 /* Type of this character set (94, 96, 94x94, 96x96) */
203 /* Number of bytes used in encoding of this character set (1 or 2) */
204 unsigned int dimension;
206 /* Number of chars in each dimension (usually 94 or 96) */
209 /* Which half of font to be used to display this character set */
210 unsigned int graphic;
213 DECLARE_LRECORD (charset, struct Lisp_Charset);
214 #define XCHARSET(x) XRECORD (x, charset, struct Lisp_Charset)
215 #define XSETCHARSET(x, p) XSETRECORD (x, p, charset)
216 #define CHARSETP(x) RECORDP (x, charset)
217 #define GC_CHARSETP(x) GC_RECORDP (x, charset)
218 #define CHECK_CHARSET(x) CHECK_RECORD (x, charset)
219 #define CONCHECK_CHARSET(x) CONCHECK_RECORD (x, charset)
221 #define CHARSET_TYPE_94 0 /* This charset includes 94 characters. */
222 #define CHARSET_TYPE_96 1 /* This charset includes 96 characters. */
223 #define CHARSET_TYPE_94X94 2 /* This charset includes 94x94 characters. */
224 #define CHARSET_TYPE_96X96 3 /* This charset includes 96x96 characters. */
225 #define CHARSET_TYPE_128X128 4 /* This charset includes 128x128 characters. */
226 #define CHARSET_TYPE_256X256 5 /* This charset includes 256x256 characters. */
228 #define CHARSET_LEFT_TO_RIGHT 0
229 #define CHARSET_RIGHT_TO_LEFT 1
231 /* Leading byte and id have been regrouped. -- OG */
232 #define CHARSET_ID(cs) ((cs)->id)
233 #define CHARSET_LEADING_BYTE(cs) ((Bufbyte)(CHARSET_ID(cs)))
234 #define CHARSET_NAME(cs) ((cs)->name)
235 #define CHARSET_SHORT_NAME(cs) ((cs)->short_name)
236 #define CHARSET_LONG_NAME(cs) ((cs)->long_name)
237 #define CHARSET_REP_BYTES(cs) ((cs)->rep_bytes)
238 #define CHARSET_COLUMNS(cs) ((cs)->columns)
239 #define CHARSET_GRAPHIC(cs) ((cs)->graphic)
240 #define CHARSET_TYPE(cs) ((cs)->type)
241 #define CHARSET_DIRECTION(cs) ((cs)->direction)
242 #define CHARSET_FINAL(cs) ((cs)->final)
243 #define CHARSET_DOC_STRING(cs) ((cs)->doc_string)
244 #define CHARSET_REGISTRY(cs) ((cs)->registry)
245 #define CHARSET_CCL_PROGRAM(cs) ((cs)->ccl_program)
246 #define CHARSET_DIMENSION(cs) ((cs)->dimension)
247 #define CHARSET_CHARS(cs) ((cs)->chars)
248 #define CHARSET_REVERSE_DIRECTION_CHARSET(cs) ((cs)->reverse_direction_charset)
251 #define XCHARSET_ID(cs) CHARSET_ID (XCHARSET (cs))
252 #define XCHARSET_NAME(cs) CHARSET_NAME (XCHARSET (cs))
253 #define XCHARSET_SHORT_NAME(cs) CHARSET_SHORT_NAME (XCHARSET (cs))
254 #define XCHARSET_LONG_NAME(cs) CHARSET_LONG_NAME (XCHARSET (cs))
255 #define XCHARSET_REP_BYTES(cs) CHARSET_REP_BYTES (XCHARSET (cs))
256 #define XCHARSET_COLUMNS(cs) CHARSET_COLUMNS (XCHARSET (cs))
257 #define XCHARSET_GRAPHIC(cs) CHARSET_GRAPHIC (XCHARSET (cs))
258 #define XCHARSET_TYPE(cs) CHARSET_TYPE (XCHARSET (cs))
259 #define XCHARSET_DIRECTION(cs) CHARSET_DIRECTION (XCHARSET (cs))
260 #define XCHARSET_FINAL(cs) CHARSET_FINAL (XCHARSET (cs))
261 #define XCHARSET_DOC_STRING(cs) CHARSET_DOC_STRING (XCHARSET (cs))
262 #define XCHARSET_REGISTRY(cs) CHARSET_REGISTRY (XCHARSET (cs))
263 #define XCHARSET_LEADING_BYTE(cs) CHARSET_LEADING_BYTE (XCHARSET (cs))
264 #define XCHARSET_CCL_PROGRAM(cs) CHARSET_CCL_PROGRAM (XCHARSET (cs))
265 #define XCHARSET_DIMENSION(cs) CHARSET_DIMENSION (XCHARSET (cs))
266 #define XCHARSET_CHARS(cs) CHARSET_CHARS (XCHARSET (cs))
267 #define XCHARSET_REVERSE_DIRECTION_CHARSET(cs) \
268 CHARSET_REVERSE_DIRECTION_CHARSET (XCHARSET (cs))
270 /* Table of charsets indexed by (leading byte - 128). */
271 extern Lisp_Object charset_by_leading_byte[NUM_LEADING_BYTES];
273 /* Table of charsets indexed by type/final-byte. */
274 extern Lisp_Object charset_by_attributes[4][128];
276 #ifdef ERROR_CHECK_TYPECHECK
277 /* int not Bufbyte even though that is the actual type of a leading byte.
278 This way, out-ot-range values will get caught rather than automatically
280 INLINE Lisp_Object CHARSET_BY_LEADING_BYTE (Charset_ID lb);
282 CHARSET_BY_LEADING_BYTE (Charset_ID lb)
284 assert (lb >= MIN_LEADING_BYTE &&
285 lb < (MIN_LEADING_BYTE + NUM_LEADING_BYTES));
286 return charset_by_leading_byte[lb - MIN_LEADING_BYTE];
291 #define CHARSET_BY_LEADING_BYTE(lb) \
292 (charset_by_leading_byte[(lb) - MIN_LEADING_BYTE])
296 #define CHARSET_BY_ATTRIBUTES(type, final, dir) \
297 (charset_by_attributes[type][final])
299 INLINE int REP_BYTES_BY_FIRST_BYTE (int fb);
301 REP_BYTES_BY_FIRST_BYTE (int fb)
305 else if ( fb < 0xe0 )
307 else if ( fb < 0xf0 )
309 else if ( fb < 0xf8 )
311 else if ( fb < 0xfc )
318 /************************************************************************/
319 /* Dealing with characters */
320 /************************************************************************/
322 #define MAX_CHAR_BASIC_LATIN 0x007F
324 #define MIN_CHAR_GREEK 0x0370
325 #define MAX_CHAR_GREEK 0x03CF
327 #define MIN_CHAR_CYRILLIC 0x0400
328 #define MAX_CHAR_CYRILLIC 0x045F
330 #define MIN_CHAR_HEBREW 0x0590
331 #define MAX_CHAR_HEBREW 0x05EF
333 #define MIN_CHAR_THAI 0x0E00
334 #define MAX_CHAR_THAI 0x0E5F
336 #define MIN_CHAR_HALFWIDTH_KATAKANA 0xFF60
337 #define MAX_CHAR_HALFWIDTH_KATAKANA 0xFF9F
339 #define MIN_CHAR_94 0xE90940
340 #define MAX_CHAR_94 (MIN_CHAR_94 + 94 * 80 - 1)
341 #define MIN_CHAR_96 (MIN_CHAR_94 + 94 * 80)
342 #define MAX_CHAR_96 (MIN_CHAR_96 + 96 * 80 - 1)
344 #define MIN_CHAR_94x94 0xE9F6C0
345 #define MAX_CHAR_94x94 (MIN_CHAR_94x94 + 94 * 94 * 80 - 1)
346 #define MIN_CHAR_96x96 0xF4C000
347 #define MAX_CHAR_96x96 (MIN_CHAR_96x96 + 96 * 96 * 80 - 1)
350 /* Return a character whose charset is CHARSET and position-codes
351 are C1 and C2. TYPE9N character ignores C2.
353 NOTE: This takes advantage of the fact that
354 FIELD2_TO_OFFICIAL_LEADING_BYTE and
355 FIELD2_TO_PRIVATE_LEADING_BYTE are the same.
358 INLINE Emchar MAKE_CHAR (Lisp_Object charset, int c1, int c2);
360 MAKE_CHAR (Lisp_Object charset, int c1, int c2)
362 if (EQ (charset, Vcharset_ascii))
364 else if (EQ (charset, Vcharset_control_1))
366 else if (EQ (charset, Vcharset_latin_iso8859_1))
368 else if (EQ (charset, Vcharset_greek_iso8859_7))
369 return c1 + MIN_CHAR_GREEK - 0x20;
370 else if (EQ (charset, Vcharset_cyrillic_iso8859_5))
371 return c1 + MIN_CHAR_CYRILLIC - 0x20;
372 else if (EQ (charset, Vcharset_hebrew_iso8859_8))
373 return c1 + MIN_CHAR_HEBREW - 0x20;
374 else if (EQ (charset, Vcharset_thai_tis620))
375 return c1 + MIN_CHAR_THAI - 0x20;
376 else if (EQ (charset, Vcharset_katakana_jisx0201))
378 return c1 + MIN_CHAR_HALFWIDTH_KATAKANA - 0x20;
381 else if (XCHARSET_DIMENSION (charset) == 1)
383 switch (XCHARSET_CHARS (charset))
387 + (XCHARSET_FINAL (charset) - '0') * 94 + (c1 - 33);
390 + (XCHARSET_FINAL (charset) - '0') * 96 + (c1 - 32);
397 switch (XCHARSET_CHARS (charset))
400 return MIN_CHAR_94x94
401 + (XCHARSET_FINAL (charset) - '0') * 94 * 94
402 + (c1 - 33) * 94 + (c2 - 33);
404 return MIN_CHAR_96x96
405 + (XCHARSET_FINAL (charset) - '0') * 96 * 96
406 + (c1 - 32) * 96 + (c2 - 32);
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];
417 INLINE void breakup_char_1 (Emchar c, Lisp_Object *charset, int *c1, int *c2);
419 breakup_char_1 (Emchar c, Lisp_Object *charset, int *c1, int *c2)
421 if (c <= MAX_CHAR_BASIC_LATIN)
423 *charset = Vcharset_ascii;
429 *charset = Vcharset_control_1;
435 *charset = Vcharset_latin_iso8859_1;
442 = CHARSET_BY_LEADING_BYTE (latin_a_char_to_charset[c - 0x100]);
443 *c1 = latin_a_char_to_byte1[c - 0x100];
444 *c2 = latin_a_char_to_byte2[c - 0x100];
446 else if (c < MIN_CHAR_GREEK)
448 printf("not break up u+%x", c);
451 else if (c <= MAX_CHAR_GREEK)
453 *charset = Vcharset_greek_iso8859_7;
454 *c1 = c - MIN_CHAR_GREEK + 0x20;
457 else if (c < MIN_CHAR_CYRILLIC)
459 printf("not break up u+%x", c);
462 else if (c <= MAX_CHAR_CYRILLIC)
464 *charset = Vcharset_cyrillic_iso8859_5;
465 *c1 = c - MIN_CHAR_CYRILLIC + 0x20;
468 else if (c < MIN_CHAR_HEBREW)
470 printf("not break up u+%x", c);
473 else if (c <= MAX_CHAR_HEBREW)
475 *charset = Vcharset_hebrew_iso8859_8;
476 *c1 = c - MIN_CHAR_HEBREW + 0x20;
479 else if (c < MIN_CHAR_THAI)
481 printf("not break up u+%x", c);
484 else if (c <= MAX_CHAR_THAI)
486 *charset = Vcharset_thai_tis620;
487 *c1 = c - MIN_CHAR_THAI + 0x20;
490 else if (c < MIN_CHAR_HALFWIDTH_KATAKANA)
492 printf("not break up u+%x", c);
495 else if (c <= MAX_CHAR_HALFWIDTH_KATAKANA)
497 *charset = Vcharset_katakana_jisx0201;
498 *c1 = c - MIN_CHAR_HALFWIDTH_KATAKANA + 0x20;
501 else if (c <= MAX_CHAR_94)
504 = CHARSET_BY_ATTRIBUTES (CHARSET_TYPE_94,
505 ((c - MIN_CHAR_94) / 94) + '0',
506 CHARSET_LEFT_TO_RIGHT);
507 *c1 = ((c - MIN_CHAR_94) % 94) + 33;
510 else if (c <= MAX_CHAR_96)
513 = CHARSET_BY_ATTRIBUTES (CHARSET_TYPE_96,
514 ((c - MIN_CHAR_96) / 96) + '0',
515 CHARSET_LEFT_TO_RIGHT);
516 *c1 = ((c - MIN_CHAR_96) % 96) + 32;
519 else if (c <= MAX_CHAR_94x94)
522 = CHARSET_BY_ATTRIBUTES (CHARSET_TYPE_94X94,
523 ((c - MIN_CHAR_94x94) / (94 * 94)) + '0',
524 CHARSET_LEFT_TO_RIGHT);
525 *c1 = (((c - MIN_CHAR_94x94) / 94) % 94) + 33;
526 *c2 = ((c - MIN_CHAR_94x94) % 94) + 33;
528 else if (c <= MAX_CHAR_96x96)
531 = CHARSET_BY_ATTRIBUTES (CHARSET_TYPE_96X96,
532 ((c - MIN_CHAR_96x96) / (96 * 96)) + '0',
533 CHARSET_LEFT_TO_RIGHT);
534 *c1 = (((c - MIN_CHAR_96x96) / 96) % 96) + 32;
535 *c2 = ((c - MIN_CHAR_96x96) % 96) + 32;
544 #define BREAKUP_CHAR(c, charset, c1, c2) \
545 breakup_char_1 (c, &(charset), &(c1), &(c2))
547 INLINE Lisp_Object CHAR_CHARSET (Emchar c);
549 CHAR_CHARSET (Emchar c)
554 BREAKUP_CHAR(c, charset, c1, c2);
558 #define CHAR_LEADING_BYTE(c) (XCHARSET_LEADING_BYTE(CHAR_CHARSET(c)))
561 #ifdef ENABLE_COMPOSITE_CHARS
562 /************************************************************************/
563 /* Composite characters */
564 /************************************************************************/
566 Emchar lookup_composite_char (Bufbyte *str, int len);
567 Lisp_Object composite_char_string (Emchar ch);
568 #endif /* ENABLE_COMPOSITE_CHARS */
571 /************************************************************************/
572 /* Exported functions */
573 /************************************************************************/
575 EXFUN (Ffind_charset, 1);
576 EXFUN (Fget_charset, 1);
578 extern Lisp_Object Vcharset_chinese_big5_1;
579 extern Lisp_Object Vcharset_chinese_big5_2;
580 extern Lisp_Object Vcharset_japanese_jisx0208;
581 extern Lisp_Object Vcharset_japanese_jisx0212;
583 Emchar Lstream_get_emchar_1 (Lstream *stream, int first_char);
584 int Lstream_fput_emchar (Lstream *stream, Emchar ch);
585 void Lstream_funget_emchar (Lstream *stream, Emchar ch);
587 int copy_internal_to_external (CONST Bufbyte *internal, Bytecount len,
588 unsigned char *external);
589 Bytecount copy_external_to_internal (CONST unsigned char *external,
590 int len, Bufbyte *internal);
592 #endif /* CHAR_IS_UCS4 */
593 #endif /* _XEMACS_CHARACTER_H */