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_CHAR_UCS_H
25 #define _XEMACS_CHAR_UCS_H
27 /* ---------------------------- */
28 /* (D) For working with Emchars */
29 /* ---------------------------- */
31 #define valid_char_p(ch) 1
33 #define CHAR_ASCII_P(c) ((c) <= 0x7F)
36 /************************************************************************/
37 /* Definition of leading bytes */
38 /************************************************************************/
40 typedef int Charset_ID;
42 #define MIN_LEADING_BYTE 0x80
44 #define LEADING_BYTE_UCS_BMP 0x80
45 #define LEADING_BYTE_CONTROL_1 0x81 /* represent normal 80-9F */
48 #define CHARSET_ID_OFFSET_94 0x60
50 #define MIN_CHARSET_ID_PRIVATE_94 (CHARSET_ID_OFFSET_94 + '0')
51 #define MAX_CHARSET_ID_PRIVATE_94 (CHARSET_ID_OFFSET_94 + '?')
54 #define LEADING_BYTE_ASCII (CHARSET_ID_OFFSET_94 + 'B')
56 /* Right half of JIS X0201-1976 */
57 #define LEADING_BYTE_KATAKANA_JISX0201 (CHARSET_ID_OFFSET_94 + 'I')
59 /* Left half of JIS X0201-1976 */
60 #define LEADING_BYTE_LATIN_JISX0201 (CHARSET_ID_OFFSET_94 + 'J')
63 #define CHARSET_ID_OFFSET_96 0x70
65 /* Right half of ISO 8859-1 */
66 #define LEADING_BYTE_LATIN_ISO8859_1 (CHARSET_ID_OFFSET_96 + 'A')
68 /* Right half of ISO 8859-2 */
69 #define LEADING_BYTE_LATIN_ISO8859_2 (CHARSET_ID_OFFSET_96 + 'B')
71 /* Right half of ISO 8859-3 */
72 #define LEADING_BYTE_LATIN_ISO8859_3 (CHARSET_ID_OFFSET_96 + 'C')
74 /* Right half of ISO 8859-4 */
75 #define LEADING_BYTE_LATIN_ISO8859_4 (CHARSET_ID_OFFSET_96 + 'D')
77 /* Right half of ISO 8859-7 */
78 #define LEADING_BYTE_GREEK_ISO8859_7 (CHARSET_ID_OFFSET_96 + 'F')
80 /* Right half of ISO 8859-6 */
81 #define LEADING_BYTE_ARABIC_ISO8859_6 (CHARSET_ID_OFFSET_96 + 'G')
83 /* Right half of ISO 8859-8 */
84 #define LEADING_BYTE_HEBREW_ISO8859_8 (CHARSET_ID_OFFSET_96 + 'H')
86 /* Right half of ISO 8859-5 */
87 #define LEADING_BYTE_CYRILLIC_ISO8859_5 (CHARSET_ID_OFFSET_96 + 'L')
89 /* Right half of ISO 8859-9 */
90 #define LEADING_BYTE_LATIN_ISO8859_9 (CHARSET_ID_OFFSET_96 + 'M')
93 #define LEADING_BYTE_THAI_TIS620 (CHARSET_ID_OFFSET_96 + 'T')
96 #define MIN_LEADING_BYTE_PRIVATE_1 0xD0
97 #define MAX_LEADING_BYTE_PRIVATE_1 0xDF
100 #define CHARSET_ID_OFFSET_94x94 0xB0
103 #define LEADING_BYTE_CHINESE_BIG5_1 ('0' + CHARSET_ID_OFFSET_94x94)
106 #define LEADING_BYTE_CHINESE_BIG5_2 ('1' + CHARSET_ID_OFFSET_94x94)
108 #define MIN_LEADING_BYTE_PRIVATE_2 ('0' + CHARSET_ID_OFFSET_94x94)
109 #define MAX_LEADING_BYTE_PRIVATE_2 ('?' + CHARSET_ID_OFFSET_94x94)
111 /* Japanese JIS X0208-1978 */
112 #define LEADING_BYTE_JAPANESE_JISX0208_1978 \
113 ('@' + CHARSET_ID_OFFSET_94x94)
115 /* Chinese Hanzi GB2312-1980 */
116 #define LEADING_BYTE_CHINESE_GB2312 ('A' + CHARSET_ID_OFFSET_94x94)
118 /* Japanese JIS X0208-1983 */
119 #define LEADING_BYTE_JAPANESE_JISX0208 ('B' + CHARSET_ID_OFFSET_94x94)
121 /* Hangul KS C5601-1987 */
122 #define LEADING_BYTE_KOREAN_KSC5601 ('C' + CHARSET_ID_OFFSET_94x94)
124 /* Japanese JIS X0212-1990 */
125 #define LEADING_BYTE_JAPANESE_JISX0212 ('D' + CHARSET_ID_OFFSET_94x94)
127 /* CCITT Extended GB */
128 #define LEADING_BYTE_CHINESE_CCITT_GB ('E' + CHARSET_ID_OFFSET_94x94)
130 /* Chinese CNS11643 Set 1 */
131 #define LEADING_BYTE_CHINESE_CNS11643_1 ('G' + CHARSET_ID_OFFSET_94x94)
133 /* Chinese CNS11643 Set 2 */
134 #define LEADING_BYTE_CHINESE_CNS11643_2 ('H' + CHARSET_ID_OFFSET_94x94)
136 /* Chinese CNS11643 Set 3 */
137 #define LEADING_BYTE_CHINESE_CNS11643_3 ('I' + CHARSET_ID_OFFSET_94x94)
139 /* Chinese CNS11643 Set 4 */
140 #define LEADING_BYTE_CHINESE_CNS11643_4 ('J' + CHARSET_ID_OFFSET_94x94)
142 /* Chinese CNS11643 Set 5 */
143 #define LEADING_BYTE_CHINESE_CNS11643_5 ('K' + CHARSET_ID_OFFSET_94x94)
145 /* Chinese CNS11643 Set 6 */
146 #define LEADING_BYTE_CHINESE_CNS11643_6 ('L' + CHARSET_ID_OFFSET_94x94)
148 /* Chinese CNS11643 Set 7 */
149 #define LEADING_BYTE_CHINESE_CNS11643_7 ('M' + CHARSET_ID_OFFSET_94x94)
151 /* DPRK Hangul KPS 9566-1997 */
152 #define LEADING_BYTE_KOREAN_KPS9566 ('N' + CHARSET_ID_OFFSET_94x94)
154 #define MIN_LEADING_BYTE_OFFICIAL_2 LEADING_BYTE_JAPANESE_JISX0208_1978
155 #define MAX_LEADING_BYTE_OFFICIAL_2 LEADING_BYTE_KOREAN_KPS9566
157 #define NUM_LEADING_BYTES 256
160 /************************************************************************/
161 /* Operations on individual bytes */
163 /************************************************************************/
165 /* Argument `c' should be (unsigned int) or (unsigned char). */
166 /* Note that SP and DEL are not included. */
168 #define BYTE_ASCII_P(c) ((c) < 0x80)
169 #define BYTE_C0_P(c) ((c) < 0x20)
170 /* Do some forced casting just to make *sure* things are gotten right. */
171 #define BYTE_C1_P(c) ((unsigned int) ((unsigned int) (c) - 0x80) < 0x20)
174 /************************************************************************/
175 /* Information about a particular character set */
176 /************************************************************************/
180 struct lcrecord_header header;
184 Lisp_Object doc_string, registry, short_name, long_name;
186 Lisp_Object reverse_direction_charset;
188 Lisp_Object ccl_program;
190 /* Final byte of this character set in ISO2022 designating escape sequence */
193 /* Number of bytes (1 - 4) required in the internal representation
194 for characters in this character set. This is *not* the
195 same as the dimension of the character set). */
196 unsigned int rep_bytes;
198 /* Number of columns a character in this charset takes up, on TTY
199 devices. Not used for X devices. */
200 unsigned int columns;
202 /* Direction of this character set */
203 unsigned int direction;
205 /* Type of this character set (94, 96, 94x94, 96x96) */
208 /* Number of bytes used in encoding of this character set (1 or 2) */
209 unsigned int dimension;
211 /* Number of chars in each dimension (usually 94 or 96) */
214 /* Which half of font to be used to display this character set */
215 unsigned int graphic;
218 DECLARE_LRECORD (charset, struct Lisp_Charset);
219 #define XCHARSET(x) XRECORD (x, charset, struct Lisp_Charset)
220 #define XSETCHARSET(x, p) XSETRECORD (x, p, charset)
221 #define CHARSETP(x) RECORDP (x, charset)
222 #define GC_CHARSETP(x) GC_RECORDP (x, charset)
223 #define CHECK_CHARSET(x) CHECK_RECORD (x, charset)
224 #define CONCHECK_CHARSET(x) CONCHECK_RECORD (x, charset)
226 #define CHARSET_TYPE_94 0 /* This charset includes 94 characters. */
227 #define CHARSET_TYPE_96 1 /* This charset includes 96 characters. */
228 #define CHARSET_TYPE_94X94 2 /* This charset includes 94x94 characters. */
229 #define CHARSET_TYPE_96X96 3 /* This charset includes 96x96 characters. */
230 #define CHARSET_TYPE_128X128 4 /* This charset includes 128x128 characters. */
231 #define CHARSET_TYPE_256X256 5 /* This charset includes 256x256 characters. */
233 #define CHARSET_LEFT_TO_RIGHT 0
234 #define CHARSET_RIGHT_TO_LEFT 1
236 /* Leading byte and id have been regrouped. -- OG */
237 #define CHARSET_ID(cs) ((cs)->id)
238 #define CHARSET_LEADING_BYTE(cs) ((Bufbyte)(CHARSET_ID(cs)))
239 #define CHARSET_NAME(cs) ((cs)->name)
240 #define CHARSET_SHORT_NAME(cs) ((cs)->short_name)
241 #define CHARSET_LONG_NAME(cs) ((cs)->long_name)
242 #define CHARSET_REP_BYTES(cs) ((cs)->rep_bytes)
243 #define CHARSET_COLUMNS(cs) ((cs)->columns)
244 #define CHARSET_GRAPHIC(cs) ((cs)->graphic)
245 #define CHARSET_TYPE(cs) ((cs)->type)
246 #define CHARSET_DIRECTION(cs) ((cs)->direction)
247 #define CHARSET_FINAL(cs) ((cs)->final)
248 #define CHARSET_DOC_STRING(cs) ((cs)->doc_string)
249 #define CHARSET_REGISTRY(cs) ((cs)->registry)
250 #define CHARSET_CCL_PROGRAM(cs) ((cs)->ccl_program)
251 #define CHARSET_DIMENSION(cs) ((cs)->dimension)
252 #define CHARSET_CHARS(cs) ((cs)->chars)
253 #define CHARSET_REVERSE_DIRECTION_CHARSET(cs) ((cs)->reverse_direction_charset)
256 #define XCHARSET_ID(cs) CHARSET_ID (XCHARSET (cs))
257 #define XCHARSET_NAME(cs) CHARSET_NAME (XCHARSET (cs))
258 #define XCHARSET_SHORT_NAME(cs) CHARSET_SHORT_NAME (XCHARSET (cs))
259 #define XCHARSET_LONG_NAME(cs) CHARSET_LONG_NAME (XCHARSET (cs))
260 #define XCHARSET_REP_BYTES(cs) CHARSET_REP_BYTES (XCHARSET (cs))
261 #define XCHARSET_COLUMNS(cs) CHARSET_COLUMNS (XCHARSET (cs))
262 #define XCHARSET_GRAPHIC(cs) CHARSET_GRAPHIC (XCHARSET (cs))
263 #define XCHARSET_TYPE(cs) CHARSET_TYPE (XCHARSET (cs))
264 #define XCHARSET_DIRECTION(cs) CHARSET_DIRECTION (XCHARSET (cs))
265 #define XCHARSET_FINAL(cs) CHARSET_FINAL (XCHARSET (cs))
266 #define XCHARSET_DOC_STRING(cs) CHARSET_DOC_STRING (XCHARSET (cs))
267 #define XCHARSET_REGISTRY(cs) CHARSET_REGISTRY (XCHARSET (cs))
268 #define XCHARSET_LEADING_BYTE(cs) CHARSET_LEADING_BYTE (XCHARSET (cs))
269 #define XCHARSET_CCL_PROGRAM(cs) CHARSET_CCL_PROGRAM (XCHARSET (cs))
270 #define XCHARSET_DIMENSION(cs) CHARSET_DIMENSION (XCHARSET (cs))
271 #define XCHARSET_CHARS(cs) CHARSET_CHARS (XCHARSET (cs))
272 #define XCHARSET_REVERSE_DIRECTION_CHARSET(cs) \
273 CHARSET_REVERSE_DIRECTION_CHARSET (XCHARSET (cs))
275 /* Table of charsets indexed by (leading byte - 128). */
276 extern Lisp_Object charset_by_leading_byte[NUM_LEADING_BYTES];
278 /* Table of charsets indexed by type/final-byte. */
279 extern Lisp_Object charset_by_attributes[4][128];
281 #ifdef ERROR_CHECK_TYPECHECK
282 /* int not Bufbyte even though that is the actual type of a leading byte.
283 This way, out-ot-range values will get caught rather than automatically
285 INLINE Lisp_Object CHARSET_BY_LEADING_BYTE (Charset_ID lb);
287 CHARSET_BY_LEADING_BYTE (Charset_ID lb)
289 assert (lb >= MIN_LEADING_BYTE &&
290 lb < (MIN_LEADING_BYTE + NUM_LEADING_BYTES));
291 return charset_by_leading_byte[lb - MIN_LEADING_BYTE];
296 #define CHARSET_BY_LEADING_BYTE(lb) \
297 (charset_by_leading_byte[(lb) - MIN_LEADING_BYTE])
301 #define CHARSET_BY_ATTRIBUTES(type, final, dir) \
302 (charset_by_attributes[type][final])
304 INLINE int REP_BYTES_BY_FIRST_BYTE (int fb);
306 REP_BYTES_BY_FIRST_BYTE (int fb)
310 else if ( fb < 0xe0 )
312 else if ( fb < 0xf0 )
314 else if ( fb < 0xf8 )
316 else if ( fb < 0xfc )
323 /************************************************************************/
324 /* Dealing with characters */
325 /************************************************************************/
327 #define MAX_CHAR_BASIC_LATIN 0x007F
329 #define MIN_CHAR_GREEK 0x0370
330 #define MAX_CHAR_GREEK 0x03CF
332 #define MIN_CHAR_CYRILLIC 0x0400
333 #define MAX_CHAR_CYRILLIC 0x045F
335 #define MIN_CHAR_HEBREW 0x0590
336 #define MAX_CHAR_HEBREW 0x05EF
338 #define MIN_CHAR_THAI 0x0E00
339 #define MAX_CHAR_THAI 0x0E5F
341 #define MIN_CHAR_HALFWIDTH_KATAKANA 0xFF60
342 #define MAX_CHAR_HALFWIDTH_KATAKANA 0xFF9F
344 #define MIN_CHAR_94 0xE90940
345 #define MAX_CHAR_94 (MIN_CHAR_94 + 94 * 80 - 1)
346 #define MIN_CHAR_96 (MIN_CHAR_94 + 94 * 80)
347 #define MAX_CHAR_96 (MIN_CHAR_96 + 96 * 80 - 1)
349 #define MIN_CHAR_94x94 0xE9F6C0
350 #define MAX_CHAR_94x94 (MIN_CHAR_94x94 + 94 * 94 * 80 - 1)
351 #define MIN_CHAR_96x96 0xF4C000
352 #define MAX_CHAR_96x96 (MIN_CHAR_96x96 + 96 * 96 * 80 - 1)
355 /* Return a character whose charset is CHARSET and position-codes
356 are C1 and C2. TYPE9N character ignores C2.
358 NOTE: This takes advantage of the fact that
359 FIELD2_TO_OFFICIAL_LEADING_BYTE and
360 FIELD2_TO_PRIVATE_LEADING_BYTE are the same.
363 INLINE Emchar MAKE_CHAR (Lisp_Object charset, int c1, int c2);
365 MAKE_CHAR (Lisp_Object charset, int c1, int c2)
367 if (EQ (charset, Vcharset_ascii))
369 else if (EQ (charset, Vcharset_control_1))
371 else if (EQ (charset, Vcharset_latin_iso8859_1))
373 else if (EQ (charset, Vcharset_greek_iso8859_7))
374 return c1 + MIN_CHAR_GREEK - 0x20;
375 else if (EQ (charset, Vcharset_cyrillic_iso8859_5))
376 return c1 + MIN_CHAR_CYRILLIC - 0x20;
377 else if (EQ (charset, Vcharset_hebrew_iso8859_8))
378 return c1 + MIN_CHAR_HEBREW - 0x20;
379 else if (EQ (charset, Vcharset_thai_tis620))
380 return c1 + MIN_CHAR_THAI - 0x20;
381 else if (EQ (charset, Vcharset_katakana_jisx0201))
383 return c1 + MIN_CHAR_HALFWIDTH_KATAKANA - 0x20;
386 else if (XCHARSET_DIMENSION (charset) == 1)
388 switch (XCHARSET_CHARS (charset))
392 + (XCHARSET_FINAL (charset) - '0') * 94 + (c1 - 33);
395 + (XCHARSET_FINAL (charset) - '0') * 96 + (c1 - 32);
402 switch (XCHARSET_CHARS (charset))
405 return MIN_CHAR_94x94
406 + (XCHARSET_FINAL (charset) - '0') * 94 * 94
407 + (c1 - 33) * 94 + (c2 - 33);
409 return MIN_CHAR_96x96
410 + (XCHARSET_FINAL (charset) - '0') * 96 * 96
411 + (c1 - 32) * 96 + (c2 - 32);
418 extern Charset_ID latin_a_char_to_charset[128];
419 extern unsigned char latin_a_char_to_byte1[128];
420 extern unsigned char latin_a_char_to_byte2[128];
422 INLINE void breakup_char_1 (Emchar c, Lisp_Object *charset, int *c1, int *c2);
424 breakup_char_1 (Emchar c, Lisp_Object *charset, int *c1, int *c2)
426 if (c <= MAX_CHAR_BASIC_LATIN)
428 *charset = Vcharset_ascii;
434 *charset = Vcharset_control_1;
440 *charset = Vcharset_latin_iso8859_1;
447 = CHARSET_BY_LEADING_BYTE (latin_a_char_to_charset[c - 0x100]);
448 *c1 = latin_a_char_to_byte1[c - 0x100];
449 *c2 = latin_a_char_to_byte2[c - 0x100];
451 else if (c < MIN_CHAR_GREEK)
453 printf("not break up u+%x", c);
456 else if (c <= MAX_CHAR_GREEK)
458 *charset = Vcharset_greek_iso8859_7;
459 *c1 = c - MIN_CHAR_GREEK + 0x20;
462 else if (c < MIN_CHAR_CYRILLIC)
464 printf("not break up u+%x", c);
467 else if (c <= MAX_CHAR_CYRILLIC)
469 *charset = Vcharset_cyrillic_iso8859_5;
470 *c1 = c - MIN_CHAR_CYRILLIC + 0x20;
473 else if (c < MIN_CHAR_HEBREW)
475 printf("not break up u+%x", c);
478 else if (c <= MAX_CHAR_HEBREW)
480 *charset = Vcharset_hebrew_iso8859_8;
481 *c1 = c - MIN_CHAR_HEBREW + 0x20;
484 else if (c < MIN_CHAR_THAI)
486 printf("not break up u+%x", c);
489 else if (c <= MAX_CHAR_THAI)
491 *charset = Vcharset_thai_tis620;
492 *c1 = c - MIN_CHAR_THAI + 0x20;
495 else if (c < MIN_CHAR_HALFWIDTH_KATAKANA)
497 printf("not break up u+%x", c);
500 else if (c <= MAX_CHAR_HALFWIDTH_KATAKANA)
502 *charset = Vcharset_katakana_jisx0201;
503 *c1 = c - MIN_CHAR_HALFWIDTH_KATAKANA + 0x20;
506 else if (c <= MAX_CHAR_94)
509 = CHARSET_BY_ATTRIBUTES (CHARSET_TYPE_94,
510 ((c - MIN_CHAR_94) / 94) + '0',
511 CHARSET_LEFT_TO_RIGHT);
512 *c1 = ((c - MIN_CHAR_94) % 94) + 33;
515 else if (c <= MAX_CHAR_96)
518 = CHARSET_BY_ATTRIBUTES (CHARSET_TYPE_96,
519 ((c - MIN_CHAR_96) / 96) + '0',
520 CHARSET_LEFT_TO_RIGHT);
521 *c1 = ((c - MIN_CHAR_96) % 96) + 32;
524 else if (c <= MAX_CHAR_94x94)
527 = CHARSET_BY_ATTRIBUTES (CHARSET_TYPE_94X94,
528 ((c - MIN_CHAR_94x94) / (94 * 94)) + '0',
529 CHARSET_LEFT_TO_RIGHT);
530 *c1 = (((c - MIN_CHAR_94x94) / 94) % 94) + 33;
531 *c2 = ((c - MIN_CHAR_94x94) % 94) + 33;
533 else if (c <= MAX_CHAR_96x96)
536 = CHARSET_BY_ATTRIBUTES (CHARSET_TYPE_96X96,
537 ((c - MIN_CHAR_96x96) / (96 * 96)) + '0',
538 CHARSET_LEFT_TO_RIGHT);
539 *c1 = (((c - MIN_CHAR_96x96) / 96) % 96) + 32;
540 *c2 = ((c - MIN_CHAR_96x96) % 96) + 32;
549 #define BREAKUP_CHAR(c, charset, c1, c2) \
550 breakup_char_1 (c, &(charset), &(c1), &(c2))
552 INLINE Lisp_Object CHAR_CHARSET (Emchar c);
554 CHAR_CHARSET (Emchar c)
559 BREAKUP_CHAR(c, charset, c1, c2);
563 #define CHAR_LEADING_BYTE(c) (XCHARSET_LEADING_BYTE(CHAR_CHARSET(c)))
566 #ifdef ENABLE_COMPOSITE_CHARS
567 /************************************************************************/
568 /* Composite characters */
569 /************************************************************************/
571 Emchar lookup_composite_char (Bufbyte *str, int len);
572 Lisp_Object composite_char_string (Emchar ch);
573 #endif /* ENABLE_COMPOSITE_CHARS */
576 /************************************************************************/
577 /* Exported functions */
578 /************************************************************************/
580 EXFUN (Ffind_charset, 1);
581 EXFUN (Fget_charset, 1);
583 extern Lisp_Object Vcharset_chinese_big5_1;
584 extern Lisp_Object Vcharset_chinese_big5_2;
585 extern Lisp_Object Vcharset_japanese_jisx0208;
586 extern Lisp_Object Vcharset_japanese_jisx0212;
588 Emchar Lstream_get_emchar_1 (Lstream *stream, int first_char);
589 int Lstream_fput_emchar (Lstream *stream, Emchar ch);
590 void Lstream_funget_emchar (Lstream *stream, Emchar ch);
592 int copy_internal_to_external (CONST Bufbyte *internal, Bytecount len,
593 unsigned char *external);
594 Bytecount copy_external_to_internal (CONST unsigned char *external,
595 int len, Bufbyte *internal);
597 #endif /* _XEMACS_CHAR_UCS_H */