]> code.delx.au - gnu-emacs/blob - src/charset.c
(multibyte_chars_in_text): New function.
[gnu-emacs] / src / charset.c
1 /* Basic multilingual character support.
2 Copyright (C) 1995, 1997 Electrotechnical Laboratory, JAPAN.
3 Licensed to the Free Software Foundation.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; 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 /* At first, see the document in `charset.h' to understand the code in
23 this file. */
24
25 #include <stdio.h>
26
27 #ifdef emacs
28
29 #include <sys/types.h>
30 #include <config.h>
31 #include "lisp.h"
32 #include "buffer.h"
33 #include "charset.h"
34 #include "coding.h"
35 #include "disptab.h"
36
37 #else /* not emacs */
38
39 #include "mulelib.h"
40
41 #endif /* emacs */
42
43 Lisp_Object Qcharset, Qascii, Qcomposition;
44
45 /* Declaration of special leading-codes. */
46 int leading_code_composition; /* for composite characters */
47 int leading_code_private_11; /* for private DIMENSION1 of 1-column */
48 int leading_code_private_12; /* for private DIMENSION1 of 2-column */
49 int leading_code_private_21; /* for private DIMENSION2 of 1-column */
50 int leading_code_private_22; /* for private DIMENSION2 of 2-column */
51
52 /* Declaration of special charsets. */
53 int charset_ascii; /* ASCII */
54 int charset_composition; /* for a composite character */
55 int charset_latin_iso8859_1; /* ISO8859-1 (Latin-1) */
56 int charset_jisx0208_1978; /* JISX0208.1978 (Japanese Kanji old set) */
57 int charset_jisx0208; /* JISX0208.1983 (Japanese Kanji) */
58 int charset_katakana_jisx0201; /* JISX0201.Kana (Japanese Katakana) */
59 int charset_latin_jisx0201; /* JISX0201.Roman (Japanese Roman) */
60 int charset_big5_1; /* Big5 Level 1 (Chinese Traditional) */
61 int charset_big5_2; /* Big5 Level 2 (Chinese Traditional) */
62
63 Lisp_Object Qcharset_table;
64
65 /* A char-table containing information of each character set. */
66 Lisp_Object Vcharset_table;
67
68 /* A vector of charset symbol indexed by charset-id. This is used
69 only for returning charset symbol from C functions. */
70 Lisp_Object Vcharset_symbol_table;
71
72 /* A list of charset symbols ever defined. */
73 Lisp_Object Vcharset_list;
74
75 /* Tables used by macros BYTES_BY_CHAR_HEAD and WIDTH_BY_CHAR_HEAD. */
76 int bytes_by_char_head[256];
77 int width_by_char_head[256];
78
79 /* Mapping table from ISO2022's charset (specified by DIMENSION,
80 CHARS, and FINAL-CHAR) to Emacs' charset. */
81 int iso_charset_table[2][2][128];
82
83 /* Table of pointers to the structure `cmpchar_info' indexed by
84 CMPCHAR-ID. */
85 struct cmpchar_info **cmpchar_table;
86 /* The current size of `cmpchar_table'. */
87 static int cmpchar_table_size;
88 /* Number of the current composite characters. */
89 int n_cmpchars;
90
91 /* Variables used locally in the macro FETCH_MULTIBYTE_CHAR. */
92 unsigned char *_fetch_multibyte_char_p;
93 int _fetch_multibyte_char_len;
94
95 #define min(X, Y) ((X) < (Y) ? (X) : (Y))
96 #define max(X, Y) ((X) > (Y) ? (X) : (Y))
97 \f
98 /* Set STR a pointer to the multi-byte form of the character C. If C
99 is not a composite character, the multi-byte form is set in WORKBUF
100 and STR points WORKBUF. The caller should allocate at least 4-byte
101 area at WORKBUF in advance. Returns the length of the multi-byte
102 form. If C is an invalid character to have a multi-byte form,
103 signal an error.
104
105 Use macro `CHAR_STRING (C, WORKBUF, STR)' instead of calling this
106 function directly if C can be an ASCII character. */
107
108 int
109 non_ascii_char_to_string (c, workbuf, str)
110 int c;
111 unsigned char *workbuf, **str;
112 {
113 int charset, c1, c2;
114
115 if (COMPOSITE_CHAR_P (c))
116 {
117 int cmpchar_id = COMPOSITE_CHAR_ID (c);
118
119 if (cmpchar_id < n_cmpchars)
120 {
121 *str = cmpchar_table[cmpchar_id]->data;
122 return cmpchar_table[cmpchar_id]->len;
123 }
124 else
125 {
126 error ("Invalid character: %d", c);
127 }
128 }
129
130 SPLIT_NON_ASCII_CHAR (c, charset, c1, c2);
131 if (!charset
132 || ! CHARSET_DEFINED_P (charset)
133 || c1 >= 0 && c1 < 32
134 || c2 >= 0 && c2 < 32)
135 error ("Invalid character: %d", c);
136
137 *str = workbuf;
138 *workbuf++ = CHARSET_LEADING_CODE_BASE (charset);
139 if (*workbuf = CHARSET_LEADING_CODE_EXT (charset))
140 workbuf++;
141 *workbuf++ = c1 | 0x80;
142 if (c2 >= 0)
143 *workbuf++ = c2 | 0x80;
144
145 return (workbuf - *str);
146 }
147
148 /* Return a non-ASCII character of which multi-byte form is at STR of
149 length LEN. If ACTUAL_LEN is not NULL, the actual length of the
150 character is set to the address ACTUAL_LEN.
151
152 Use macro `STRING_CHAR (STR, LEN)' instead of calling this function
153 directly if STR can hold an ASCII character. */
154
155 string_to_non_ascii_char (str, len, actual_len)
156 unsigned char *str;
157 int len, *actual_len;
158 {
159 int charset;
160 unsigned char c1, c2;
161 register int c;
162
163 if (SPLIT_STRING (str, len, charset, c1, c2) == CHARSET_ASCII)
164 {
165 if (actual_len)
166 *actual_len = 1;
167 return (int) *str;
168 }
169
170 c = MAKE_NON_ASCII_CHAR (charset, c1, c2);
171
172 if (actual_len)
173 *actual_len = (charset == CHARSET_COMPOSITION
174 ? cmpchar_table[COMPOSITE_CHAR_ID (c)]->len
175 : BYTES_BY_CHAR_HEAD (*str));
176 return c;
177 }
178
179 /* Return the length of the multi-byte form at string STR of length LEN. */
180 int
181 multibyte_form_length (str, len)
182 unsigned char *str;
183 int len;
184 {
185 int charset;
186 unsigned char c1, c2;
187 register int c;
188
189 if (SPLIT_STRING (str, len, charset, c1, c2) == CHARSET_ASCII)
190 return 1;
191
192 return (charset == CHARSET_COMPOSITION
193 ? cmpchar_table[(c1 << 7) | c2]->len
194 : BYTES_BY_CHAR_HEAD (*str));
195 }
196
197 /* Check if string STR of length LEN contains valid multi-byte form of
198 a character. If valid, charset and position codes of the character
199 is set at *CHARSET, *C1, and *C2, and return 0. If not valid,
200 return -1. This should be used only in the macro SPLIT_STRING
201 which checks range of STR in advance. */
202
203 split_non_ascii_string (str, len, charset, c1, c2)
204 register unsigned char *str, *c1, *c2;
205 register int len, *charset;
206 {
207 register unsigned int cs = *str++;
208
209 if (cs == LEADING_CODE_COMPOSITION)
210 {
211 int cmpchar_id = str_cmpchar_id (str - 1, len);
212
213 if (cmpchar_id < 0)
214 return -1;
215 *charset = cs, *c1 = cmpchar_id >> 7, *c2 = cmpchar_id & 0x7F;
216 }
217 else if ((cs < LEADING_CODE_PRIVATE_11 || (cs = *str++) >= 0xA0)
218 && CHARSET_DEFINED_P (cs))
219 {
220 *charset = cs;
221 if (*str < 0xA0)
222 return -1;
223 *c1 = (*str++) & 0x7F;
224 if (CHARSET_DIMENSION (cs) == 2)
225 {
226 if (*str < 0xA0)
227 return -1;
228 *c2 = (*str++) & 0x7F;
229 }
230 }
231 else
232 return -1;
233 return 0;
234 }
235
236 /* Return a character unified with C (or a character made of CHARSET,
237 C1, and C2) in unification table TABLE. If no unification is found
238 in TABLE, return C. */
239 unify_char (table, c, charset, c1, c2)
240 Lisp_Object table;
241 int c, charset, c1, c2;
242 {
243 Lisp_Object ch;
244 int alt_charset, alt_c1, alt_c2, dimension;
245
246 if (c < 0) c = MAKE_CHAR (charset, c1, c2);
247 if (!CHAR_TABLE_P (table)
248 || (ch = Faref (table, make_number (c)), !INTEGERP (ch))
249 || XINT (ch) < 0)
250 return c;
251
252 SPLIT_CHAR (XFASTINT (ch), alt_charset, alt_c1, alt_c2);
253 dimension = CHARSET_DIMENSION (alt_charset);
254 if (dimension == 1 && alt_c1 > 0 || dimension == 2 && alt_c2 > 0)
255 /* CH is not a generic character, just return it. */
256 return XFASTINT (ch);
257
258 /* Since CH is a generic character, we must return a specific
259 charater which has the same position codes as C from CH. */
260 if (charset < 0)
261 SPLIT_CHAR (c, charset, c1, c2);
262 if (dimension != CHARSET_DIMENSION (charset))
263 /* We can't make such a character because of dimension mismatch. */
264 return c;
265 if (!alt_c1) alt_c1 = c1;
266 if (!alt_c2) alt_c2 = c2;
267 return MAKE_CHAR (alt_charset, c1, c2);
268 }
269
270 /* Update the table Vcharset_table with the given arguments (see the
271 document of `define-charset' for the meaning of each argument).
272 Several other table contents are also updated. The caller should
273 check the validity of CHARSET-ID and the remaining arguments in
274 advance. */
275
276 void
277 update_charset_table (charset_id, dimension, chars, width, direction,
278 iso_final_char, iso_graphic_plane,
279 short_name, long_name, description)
280 Lisp_Object charset_id, dimension, chars, width, direction;
281 Lisp_Object iso_final_char, iso_graphic_plane;
282 Lisp_Object short_name, long_name, description;
283 {
284 int charset = XINT (charset_id);
285 int bytes;
286 unsigned char leading_code_base, leading_code_ext;
287
288 if (NILP (CHARSET_TABLE_ENTRY (charset)))
289 CHARSET_TABLE_ENTRY (charset)
290 = Fmake_vector (make_number (CHARSET_MAX_IDX), Qnil);
291
292 /* Get byte length of multibyte form, base leading-code, and
293 extended leading-code of the charset. See the comment under the
294 title "GENERAL NOTE on CHARACTER SET (CHARSET)" in charset.h. */
295 bytes = XINT (dimension);
296 if (charset < MIN_CHARSET_PRIVATE_DIMENSION1)
297 {
298 /* Official charset, it doesn't have an extended leading-code. */
299 if (charset != CHARSET_ASCII)
300 bytes += 1; /* For a base leading-code. */
301 leading_code_base = charset;
302 leading_code_ext = 0;
303 }
304 else
305 {
306 /* Private charset. */
307 bytes += 2; /* For base and extended leading-codes. */
308 leading_code_base
309 = (charset < LEADING_CODE_EXT_12
310 ? LEADING_CODE_PRIVATE_11
311 : (charset < LEADING_CODE_EXT_21
312 ? LEADING_CODE_PRIVATE_12
313 : (charset < LEADING_CODE_EXT_22
314 ? LEADING_CODE_PRIVATE_21
315 : LEADING_CODE_PRIVATE_22)));
316 leading_code_ext = charset;
317 }
318
319 CHARSET_TABLE_INFO (charset, CHARSET_ID_IDX) = charset_id;
320 CHARSET_TABLE_INFO (charset, CHARSET_BYTES_IDX) = make_number (bytes);
321 CHARSET_TABLE_INFO (charset, CHARSET_DIMENSION_IDX) = dimension;
322 CHARSET_TABLE_INFO (charset, CHARSET_CHARS_IDX) = chars;
323 CHARSET_TABLE_INFO (charset, CHARSET_WIDTH_IDX) = width;
324 CHARSET_TABLE_INFO (charset, CHARSET_DIRECTION_IDX) = direction;
325 CHARSET_TABLE_INFO (charset, CHARSET_LEADING_CODE_BASE_IDX)
326 = make_number (leading_code_base);
327 CHARSET_TABLE_INFO (charset, CHARSET_LEADING_CODE_EXT_IDX)
328 = make_number (leading_code_ext);
329 CHARSET_TABLE_INFO (charset, CHARSET_ISO_FINAL_CHAR_IDX) = iso_final_char;
330 CHARSET_TABLE_INFO (charset, CHARSET_ISO_GRAPHIC_PLANE_IDX)
331 = iso_graphic_plane;
332 CHARSET_TABLE_INFO (charset, CHARSET_SHORT_NAME_IDX) = short_name;
333 CHARSET_TABLE_INFO (charset, CHARSET_LONG_NAME_IDX) = long_name;
334 CHARSET_TABLE_INFO (charset, CHARSET_DESCRIPTION_IDX) = description;
335 CHARSET_TABLE_INFO (charset, CHARSET_PLIST_IDX) = Qnil;
336
337 {
338 /* If we have already defined a charset which has the same
339 DIMENSION, CHARS and ISO-FINAL-CHAR but the different
340 DIRECTION, we must update the entry REVERSE-CHARSET of both
341 charsets. If there's no such charset, the value of the entry
342 is set to nil. */
343 int i;
344
345 for (i = 0; i <= MAX_CHARSET; i++)
346 if (!NILP (CHARSET_TABLE_ENTRY (i)))
347 {
348 if (CHARSET_DIMENSION (i) == XINT (dimension)
349 && CHARSET_CHARS (i) == XINT (chars)
350 && CHARSET_ISO_FINAL_CHAR (i) == XINT (iso_final_char)
351 && CHARSET_DIRECTION (i) != XINT (direction))
352 {
353 CHARSET_TABLE_INFO (charset, CHARSET_REVERSE_CHARSET_IDX)
354 = make_number (i);
355 CHARSET_TABLE_INFO (i, CHARSET_REVERSE_CHARSET_IDX) = charset_id;
356 break;
357 }
358 }
359 if (i > MAX_CHARSET)
360 /* No such a charset. */
361 CHARSET_TABLE_INFO (charset, CHARSET_REVERSE_CHARSET_IDX)
362 = make_number (-1);
363 }
364
365 if (charset != CHARSET_ASCII
366 && charset < MIN_CHARSET_PRIVATE_DIMENSION1)
367 {
368 /* Update tables bytes_by_char_head and width_by_char_head. */
369 bytes_by_char_head[leading_code_base] = bytes;
370 width_by_char_head[leading_code_base] = XINT (width);
371
372 /* Update table emacs_code_class. */
373 emacs_code_class[charset] = (bytes == 2
374 ? EMACS_leading_code_2
375 : (bytes == 3
376 ? EMACS_leading_code_3
377 : EMACS_leading_code_4));
378 }
379
380 /* Update table iso_charset_table. */
381 if (ISO_CHARSET_TABLE (dimension, chars, iso_final_char) < 0)
382 ISO_CHARSET_TABLE (dimension, chars, iso_final_char) = charset;
383 }
384
385 #ifdef emacs
386
387 /* Return charset id of CHARSET_SYMBOL, or return -1 if CHARSET_SYMBOL
388 is invalid. */
389 int
390 get_charset_id (charset_symbol)
391 Lisp_Object charset_symbol;
392 {
393 Lisp_Object val;
394 int charset;
395
396 return ((SYMBOLP (charset_symbol)
397 && (val = Fget (charset_symbol, Qcharset), VECTORP (val))
398 && (charset = XINT (XVECTOR (val)->contents[CHARSET_ID_IDX]),
399 CHARSET_VALID_P (charset)))
400 ? charset : -1);
401 }
402
403 /* Return an identification number for a new private charset of
404 DIMENSION and WIDTH. If there's no more room for the new charset,
405 return 0. */
406 Lisp_Object
407 get_new_private_charset_id (dimension, width)
408 int dimension, width;
409 {
410 int charset, from, to;
411
412 if (dimension == 1)
413 {
414 if (width == 1)
415 from = LEADING_CODE_EXT_11, to = LEADING_CODE_EXT_12;
416 else
417 from = LEADING_CODE_EXT_12, to = LEADING_CODE_EXT_21;
418 }
419 else
420 {
421 if (width == 1)
422 from = LEADING_CODE_EXT_21, to = LEADING_CODE_EXT_22;
423 else
424 from = LEADING_CODE_EXT_22, to = LEADING_CODE_EXT_MAX - 1;
425 }
426
427 for (charset = from; charset < to; charset++)
428 if (!CHARSET_DEFINED_P (charset)) break;
429
430 return make_number (charset < to ? charset : 0);
431 }
432
433 DEFUN ("define-charset", Fdefine_charset, Sdefine_charset, 3, 3, 0,
434 "Define CHARSET-ID as the identification number of CHARSET with INFO-VECTOR.\n\
435 If CHARSET-ID is nil, it is decided automatically, which means CHARSET is\n\
436 treated as a private charset.\n\
437 INFO-VECTOR is a vector of the format:\n\
438 [DIMENSION CHARS WIDTH DIRECTION ISO-FINAL-CHAR ISO-GRAPHIC-PLANE\n\
439 SHORT-NAME LONG-NAME DESCRIPTION]\n\
440 The meanings of each elements is as follows:\n\
441 DIMENSION (integer) is the number of bytes to represent a character: 1 or 2.\n\
442 CHARS (integer) is the number of characters in a dimension: 94 or 96.\n\
443 WIDTH (integer) is the number of columns a character in the charset\n\
444 occupies on the screen: one of 0, 1, and 2.\n\
445 \n\
446 DIRECTION (integer) is the rendering direction of characters in the\n\
447 charset when rendering. If 0, render from right to left, else\n\
448 render from left to right.\n\
449 \n\
450 ISO-FINAL-CHAR (character) is the final character of the\n\
451 corresponding ISO 2022 charset.\n\
452 \n\
453 ISO-GRAPHIC-PLANE (integer) is the graphic plane to be invoked\n\
454 while encoding to variants of ISO 2022 coding system, one of the\n\
455 following: 0/graphic-plane-left(GL), 1/graphic-plane-right(GR).\n\
456 \n\
457 SHORT-NAME (string) is the short name to refer to the charset.\n\
458 \n\
459 LONG-NAME (string) is the long name to refer to the charset.\n\
460 \n\
461 DESCRIPTION (string) is the description string of the charset.")
462 (charset_id, charset_symbol, info_vector)
463 Lisp_Object charset_id, charset_symbol, info_vector;
464 {
465 Lisp_Object *vec;
466
467 if (!NILP (charset_id))
468 CHECK_NUMBER (charset_id, 0);
469 CHECK_SYMBOL (charset_symbol, 1);
470 CHECK_VECTOR (info_vector, 2);
471
472 if (! NILP (charset_id))
473 {
474 if (! CHARSET_VALID_P (XINT (charset_id)))
475 error ("Invalid CHARSET: %d", XINT (charset_id));
476 else if (CHARSET_DEFINED_P (XINT (charset_id)))
477 error ("Already defined charset: %d", XINT (charset_id));
478 }
479
480 vec = XVECTOR (info_vector)->contents;
481 if (XVECTOR (info_vector)->size != 9
482 || !INTEGERP (vec[0]) || !(XINT (vec[0]) == 1 || XINT (vec[0]) == 2)
483 || !INTEGERP (vec[1]) || !(XINT (vec[1]) == 94 || XINT (vec[1]) == 96)
484 || !INTEGERP (vec[2]) || !(XINT (vec[2]) == 1 || XINT (vec[2]) == 2)
485 || !INTEGERP (vec[3]) || !(XINT (vec[3]) == 0 || XINT (vec[3]) == 1)
486 || !INTEGERP (vec[4]) || !(XINT (vec[4]) >= '0' && XINT (vec[4]) <= '~')
487 || !INTEGERP (vec[5]) || !(XINT (vec[5]) == 0 || XINT (vec[5]) == 1)
488 || !STRINGP (vec[6])
489 || !STRINGP (vec[7])
490 || !STRINGP (vec[8]))
491 error ("Invalid info-vector argument for defining charset %s",
492 XSYMBOL (charset_symbol)->name->data);
493
494 if (NILP (charset_id))
495 {
496 charset_id = get_new_private_charset_id (XINT (vec[0]), XINT (vec[2]));
497 if (XINT (charset_id) == 0)
498 error ("There's no room for a new private charset %s",
499 XSYMBOL (charset_symbol)->name->data);
500 }
501
502 update_charset_table (charset_id, vec[0], vec[1], vec[2], vec[3],
503 vec[4], vec[5], vec[6], vec[7], vec[8]);
504 Fput (charset_symbol, Qcharset, CHARSET_TABLE_ENTRY (XINT (charset_id)));
505 CHARSET_SYMBOL (XINT (charset_id)) = charset_symbol;
506 Vcharset_list = Fcons (charset_symbol, Vcharset_list);
507 return Qnil;
508 }
509
510 DEFUN ("get-unused-iso-final-char", Fget_unused_iso_final_char,
511 Sget_unused_iso_final_char, 2, 2, 0,
512 "Return an unsed ISO's final char for a charset of DIMENISION and CHARS.\n\
513 DIMENSION is the number of bytes to represent a character: 1 or 2.\n\
514 CHARS is the number of characters in a dimension: 94 or 96.\n\
515 \n\
516 This final char is for private use, thus the range is `0' (48) .. `?' (63).\n\
517 If there's no unused final char for the specified kind of charset,\n\
518 return nil.")
519 (dimension, chars)
520 Lisp_Object dimension, chars;
521 {
522 int final_char;
523
524 CHECK_NUMBER (dimension, 0);
525 CHECK_NUMBER (chars, 1);
526 if (XINT (dimension) != 1 && XINT (dimension) != 2)
527 error ("Invalid charset dimension %d, it should be 1 or 2",
528 XINT (dimension));
529 if (XINT (chars) != 94 && XINT (chars) != 96)
530 error ("Invalid charset chars %d, it should be 94 or 96",
531 XINT (chars));
532 for (final_char = '0'; final_char <= '?'; final_char++)
533 {
534 if (ISO_CHARSET_TABLE (dimension, chars, make_number (final_char)) < 0)
535 break;
536 }
537 return (final_char <= '?' ? make_number (final_char) : Qnil);
538 }
539
540 DEFUN ("declare-equiv-charset", Fdeclare_equiv_charset, Sdeclare_equiv_charset,
541 4, 4, 0,
542 "Declare a charset of DIMENSION, CHARS, FINAL-CHAR is the same as CHARSET.\n\
543 CHARSET should be defined by `defined-charset' in advance.")
544 (dimension, chars, final_char, charset_symbol)
545 Lisp_Object dimension, chars, final_char, charset_symbol;
546 {
547 int charset;
548
549 CHECK_NUMBER (dimension, 0);
550 CHECK_NUMBER (chars, 1);
551 CHECK_NUMBER (final_char, 2);
552 CHECK_SYMBOL (charset_symbol, 3);
553
554 if (XINT (dimension) != 1 && XINT (dimension) != 2)
555 error ("Invalid DIMENSION %d, it should be 1 or 2", XINT (dimension));
556 if (XINT (chars) != 94 && XINT (chars) != 96)
557 error ("Invalid CHARS %d, it should be 94 or 96", XINT (chars));
558 if (XINT (final_char) < '0' || XFASTINT (final_char) > '~')
559 error ("Invalid FINAL-CHAR %c, it should be `0'..`~'", XINT (chars));
560 if ((charset = get_charset_id (charset_symbol)) < 0)
561 error ("Invalid charset %s", XSYMBOL (charset_symbol)->name->data);
562
563 ISO_CHARSET_TABLE (dimension, chars, final_char) = charset;
564 return Qnil;
565 }
566
567 /* Return number of different charsets in STR of length LEN. In
568 addition, for each found charset N, CHARSETS[N] is set 1. The
569 caller should allocate CHARSETS (MAX_CHARSET + 1 elements) in advance.
570 It may lookup a unification table TABLE if supplied. */
571
572 int
573 find_charset_in_str (str, len, charsets, table)
574 unsigned char *str;
575 int len, *charsets;
576 Lisp_Object table;
577 {
578 register int num = 0, c;
579
580 if (! CHAR_TABLE_P (table))
581 table = Qnil;
582
583 while (len > 0)
584 {
585 int bytes, charset;
586 c = *str;
587
588 if (c == LEADING_CODE_COMPOSITION)
589 {
590 int cmpchar_id = str_cmpchar_id (str, len);
591 GLYPH *glyph;
592
593 if (cmpchar_id > 0)
594 {
595 struct cmpchar_info *cmpcharp = cmpchar_table[cmpchar_id];
596 int i;
597
598 for (i = 0; i < cmpcharp->glyph_len; i++)
599 {
600 c = cmpcharp->glyph[i];
601 if (!NILP (table))
602 {
603 if ((c = unify_char (table, c, 0, 0, 0)) < 0)
604 c = cmpcharp->glyph[i];
605 }
606 if ((charset = CHAR_CHARSET (c)) < 0)
607 charset = CHARSET_ASCII;
608 if (!charsets[charset])
609 {
610 charsets[charset] = 1;
611 num += 1;
612 }
613 }
614 str += cmpcharp->len;
615 len -= cmpcharp->len;
616 continue;
617 }
618
619 charset = CHARSET_ASCII;
620 bytes = 1;
621 }
622 else
623 {
624 c = STRING_CHAR_AND_LENGTH (str, len, bytes);
625 if (! NILP (table))
626 {
627 int c1 = unify_char (table, c, 0, 0, 0);
628 if (c1 >= 0)
629 c = c1;
630 }
631 charset = CHAR_CHARSET (c);
632 }
633
634 if (!charsets[charset])
635 {
636 charsets[charset] = 1;
637 num += 1;
638 }
639 str += bytes;
640 len -= bytes;
641 }
642 return num;
643 }
644
645 DEFUN ("find-charset-region", Ffind_charset_region, Sfind_charset_region,
646 2, 3, 0,
647 "Return a list of charsets in the region between BEG and END.\n\
648 BEG and END are buffer positions.\n\
649 Optional arg TABLE if non-nil is a unification table to look up.")
650 (beg, end, table)
651 Lisp_Object beg, end, table;
652 {
653 int charsets[MAX_CHARSET + 1];
654 int from, from_byte, to, stop, stop_byte, i;
655 Lisp_Object val;
656
657 validate_region (&beg, &end);
658 from = XFASTINT (beg);
659 stop = to = XFASTINT (end);
660
661 if (from < GPT && GPT < to)
662 {
663 stop = GPT;
664 stop_byte = GPT_BYTE;
665 }
666 else
667 stop_byte = CHAR_TO_BYTE (stop);
668
669 from_byte = CHAR_TO_BYTE (from);
670
671 bzero (charsets, (MAX_CHARSET + 1) * sizeof (int));
672 while (1)
673 {
674 find_charset_in_str (BYTE_POS_ADDR (from_byte), stop_byte - from_byte,
675 charsets, table);
676 if (stop < to)
677 {
678 from = stop, from_byte = stop_byte;
679 stop = to, stop_byte = CHAR_TO_BYTE (stop);
680 }
681 else
682 break;
683 }
684
685 val = Qnil;
686 for (i = MAX_CHARSET; i >= 0; i--)
687 if (charsets[i])
688 val = Fcons (CHARSET_SYMBOL (i), val);
689 return val;
690 }
691
692 DEFUN ("find-charset-string", Ffind_charset_string, Sfind_charset_string,
693 1, 2, 0,
694 "Return a list of charsets in STR.\n\
695 Optional arg TABLE if non-nil is a unification table to look up.")
696 (str, table)
697 Lisp_Object str, table;
698 {
699 int charsets[MAX_CHARSET + 1];
700 int i;
701 Lisp_Object val;
702
703 CHECK_STRING (str, 0);
704
705 if (! STRING_MULTIBYTE (str))
706 return Qnil;
707
708 bzero (charsets, (MAX_CHARSET + 1) * sizeof (int));
709 find_charset_in_str (XSTRING (str)->data, XSTRING (str)->size_byte,
710 charsets, table);
711 val = Qnil;
712 for (i = MAX_CHARSET; i >= 0; i--)
713 if (charsets[i])
714 val = Fcons (CHARSET_SYMBOL (i), val);
715 return val;
716 }
717 \f
718 DEFUN ("make-char-internal", Fmake_char_internal, Smake_char_internal, 1, 3, 0,
719 "")
720 (charset, code1, code2)
721 Lisp_Object charset, code1, code2;
722 {
723 CHECK_NUMBER (charset, 0);
724
725 if (NILP (code1))
726 XSETFASTINT (code1, 0);
727 else
728 CHECK_NUMBER (code1, 1);
729 if (NILP (code2))
730 XSETFASTINT (code2, 0);
731 else
732 CHECK_NUMBER (code2, 2);
733
734 if (!CHARSET_DEFINED_P (XINT (charset)))
735 error ("Invalid charset: %d", XINT (charset));
736
737 return make_number (MAKE_CHAR (XINT (charset), XINT (code1), XINT (code2)));
738 }
739
740 DEFUN ("split-char", Fsplit_char, Ssplit_char, 1, 1, 0,
741 "Return list of charset and one or two position-codes of CHAR.")
742 (ch)
743 Lisp_Object ch;
744 {
745 Lisp_Object val;
746 int charset, c1, c2;
747
748 CHECK_NUMBER (ch, 0);
749 SPLIT_CHAR (XFASTINT (ch), charset, c1, c2);
750 return (c2 >= 0
751 ? Fcons (CHARSET_SYMBOL (charset),
752 Fcons (make_number (c1), Fcons (make_number (c2), Qnil)))
753 : Fcons (CHARSET_SYMBOL (charset), Fcons (make_number (c1), Qnil)));
754 }
755
756 DEFUN ("char-charset", Fchar_charset, Schar_charset, 1, 1, 0,
757 "Return charset of CHAR.")
758 (ch)
759 Lisp_Object ch;
760 {
761 CHECK_NUMBER (ch, 0);
762
763 return CHARSET_SYMBOL (CHAR_CHARSET (XINT (ch)));
764 }
765
766 DEFUN ("iso-charset", Fiso_charset, Siso_charset, 3, 3, 0,
767 "Return charset of ISO's specification DIMENSION, CHARS, and FINAL-CHAR.")
768 (dimension, chars, final_char)
769 Lisp_Object dimension, chars, final_char;
770 {
771 int charset;
772
773 CHECK_NUMBER (dimension, 0);
774 CHECK_NUMBER (chars, 1);
775 CHECK_NUMBER (final_char, 2);
776
777 if ((charset = ISO_CHARSET_TABLE (dimension, chars, final_char)) < 0)
778 return Qnil;
779 return CHARSET_SYMBOL (charset);
780 }
781
782 /* If GENERICP is nonzero, return nonzero iff C is a valid normal or
783 generic character. If GENERICP is zero, return nonzero iff C is a
784 valid normal character. Do not call this function directly,
785 instead use macro CHAR_VALID_P. */
786 int
787 char_valid_p (c, genericp)
788 int c, genericp;
789 {
790 int charset, c1, c2;
791
792 if (c < 0)
793 return 0;
794 if (SINGLE_BYTE_CHAR_P (c))
795 return 1;
796 SPLIT_NON_ASCII_CHAR (c, charset, c1, c2);
797 if (!CHARSET_VALID_P (charset))
798 return 0;
799 return (c < MIN_CHAR_COMPOSITION
800 ? ((c & CHAR_FIELD1_MASK) /* i.e. dimension of C is two. */
801 ? (genericp && c1 == 0 && c2 == 0
802 || c1 >= 32 && c2 >= 32)
803 : (genericp && c1 == 0
804 || c1 >= 32))
805 : c < MIN_CHAR_COMPOSITION + n_cmpchars);
806 }
807
808 DEFUN ("char-valid-p", Fchar_valid_p, Schar_valid_p, 1, 2, 0,
809 "Return t if OBJECT is a valid normal character.
810 If optional arg GENERICP is non-nil, also return t if OBJECT is
811 a valid generic character.")
812 (object, genericp)
813 Lisp_Object object, genericp;
814 {
815 if (! NATNUMP (object))
816 return Qnil;
817 return (CHAR_VALID_P (XFASTINT (object), !NILP (genericp)) ? Qt : Qnil);
818 }
819
820 DEFUN ("char-bytes", Fchar_bytes, Schar_bytes, 1, 1, 0,
821 "Return byte length of multi-byte form of CHAR.")
822 (ch)
823 Lisp_Object ch;
824 {
825 Lisp_Object val;
826 int bytes;
827
828 CHECK_NUMBER (ch, 0);
829 if (COMPOSITE_CHAR_P (XFASTINT (ch)))
830 {
831 unsigned int id = COMPOSITE_CHAR_ID (XFASTINT (ch));
832
833 bytes = (id < n_cmpchars ? cmpchar_table[id]->len : 1);
834 }
835 else
836 {
837 int charset = CHAR_CHARSET (XFASTINT (ch));
838
839 bytes = CHARSET_DEFINED_P (charset) ? CHARSET_BYTES (charset) : 1;
840 }
841
842 XSETFASTINT (val, bytes);
843 return val;
844 }
845
846 /* Return the width of character of which multi-byte form starts with
847 C. The width is measured by how many columns occupied on the
848 screen when displayed in the current buffer. */
849
850 #define ONE_BYTE_CHAR_WIDTH(c) \
851 (c < 0x20 \
852 ? (c == '\t' \
853 ? XFASTINT (current_buffer->tab_width) \
854 : (c == '\n' ? 0 : (NILP (current_buffer->ctl_arrow) ? 4 : 2))) \
855 : (c < 0x7f \
856 ? 1 \
857 : (c == 0x7F \
858 ? (NILP (current_buffer->ctl_arrow) ? 4 : 2) \
859 : ((! NILP (current_buffer->enable_multibyte_characters) \
860 && BASE_LEADING_CODE_P (c)) \
861 ? WIDTH_BY_CHAR_HEAD (c) \
862 : 4)))) \
863
864
865 DEFUN ("char-width", Fchar_width, Schar_width, 1, 1, 0,
866 "Return width of CHAR when displayed in the current buffer.\n\
867 The width is measured by how many columns it occupies on the screen.")
868 (ch)
869 Lisp_Object ch;
870 {
871 Lisp_Object val, disp;
872 int c;
873 struct Lisp_Char_Table *dp = buffer_display_table ();
874
875 CHECK_NUMBER (ch, 0);
876
877 c = XINT (ch);
878
879 /* Get the way the display table would display it. */
880 disp = dp ? DISP_CHAR_VECTOR (dp, c) : Qnil;
881
882 if (VECTORP (disp))
883 XSETINT (val, XVECTOR (disp)->size);
884 else if (SINGLE_BYTE_CHAR_P (c))
885 XSETINT (val, ONE_BYTE_CHAR_WIDTH (c));
886 else if (COMPOSITE_CHAR_P (c))
887 {
888 int id = COMPOSITE_CHAR_ID (XFASTINT (ch));
889 XSETFASTINT (val, (id < n_cmpchars ? cmpchar_table[id]->width : 0));
890 }
891 else
892 {
893 int charset = CHAR_CHARSET (c);
894
895 XSETFASTINT (val, CHARSET_WIDTH (charset));
896 }
897 return val;
898 }
899
900 /* Return width of string STR of length LEN when displayed in the
901 current buffer. The width is measured by how many columns it
902 occupies on the screen. */
903
904 int
905 strwidth (str, len)
906 unsigned char *str;
907 int len;
908 {
909 unsigned char *endp = str + len;
910 int width = 0;
911 struct Lisp_Char_Table *dp = buffer_display_table ();
912
913 while (str < endp)
914 {
915 if (*str == LEADING_CODE_COMPOSITION)
916 {
917 int id = str_cmpchar_id (str, endp - str);
918
919 if (id < 0)
920 {
921 width += 4;
922 str++;
923 }
924 else
925 {
926 width += cmpchar_table[id]->width;
927 str += cmpchar_table[id]->len;
928 }
929 }
930 else
931 {
932 Lisp_Object disp;
933 int thiswidth;
934 int c = STRING_CHAR (str, endp - str);
935
936 /* Get the way the display table would display it. */
937 if (dp)
938 disp = DISP_CHAR_VECTOR (dp, c);
939 else
940 disp = Qnil;
941
942 if (VECTORP (disp))
943 thiswidth = XVECTOR (disp)->size;
944 else
945 thiswidth = ONE_BYTE_CHAR_WIDTH (*str);
946
947 width += thiswidth;
948 str += BYTES_BY_CHAR_HEAD (*str);
949 }
950 }
951 return width;
952 }
953
954 DEFUN ("string-width", Fstring_width, Sstring_width, 1, 1, 0,
955 "Return width of STRING when displayed in the current buffer.\n\
956 Width is measured by how many columns it occupies on the screen.\n\
957 When calculating width of a multibyte character in STRING,\n\
958 only the base leading-code is considered; the validity of\n\
959 the following bytes is not checked.")
960 (str)
961 Lisp_Object str;
962 {
963 Lisp_Object val;
964
965 CHECK_STRING (str, 0);
966 XSETFASTINT (val, strwidth (XSTRING (str)->data, XSTRING (str)->size));
967 return val;
968 }
969
970 DEFUN ("char-direction", Fchar_direction, Schar_direction, 1, 1, 0,
971 "Return the direction of CHAR.\n\
972 The returned value is 0 for left-to-right and 1 for right-to-left.")
973 (ch)
974 Lisp_Object ch;
975 {
976 int charset;
977
978 CHECK_NUMBER (ch, 0);
979 charset = CHAR_CHARSET (XFASTINT (ch));
980 if (!CHARSET_DEFINED_P (charset))
981 error ("Invalid character: %d", XINT (ch));
982 return CHARSET_TABLE_INFO (charset, CHARSET_DIRECTION_IDX);
983 }
984
985 DEFUN ("chars-in-region", Fchars_in_region, Schars_in_region, 2, 2, 0,
986 "Return number of characters between BEG and END.")
987 (beg, end)
988 Lisp_Object beg, end;
989 {
990 int from, to;
991
992 from = min (XFASTINT (beg), XFASTINT (end));
993 to = max (XFASTINT (beg), XFASTINT (end));
994
995 return to - from;
996 }
997
998 /* Return the number of characters in the NBYTES bytes at PTR.
999 This works by looking at the contents and checking for multibyte sequences.
1000 However, if the current buffer has enable-multibyte-characters = nil,
1001 we treat each byte as a character. */
1002
1003 int
1004 chars_in_text (ptr, nbytes)
1005 unsigned char *ptr;
1006 int nbytes;
1007 {
1008 unsigned char *endp;
1009 int chars;
1010
1011 /* current_buffer is null at early stages of Emacs initialization. */
1012 if (current_buffer == 0
1013 || NILP (current_buffer->enable_multibyte_characters))
1014 return nbytes;
1015
1016 endp = ptr + nbytes;
1017 chars = 0;
1018
1019 while (ptr < endp)
1020 {
1021 if (*ptr == LEADING_CODE_COMPOSITION)
1022 {
1023 ptr++;
1024 while (ptr < endp && ! CHAR_HEAD_P (*ptr)) ptr++;
1025 }
1026 else
1027 ptr += BYTES_BY_CHAR_HEAD (*ptr);
1028 chars++;
1029 }
1030
1031 return chars;
1032 }
1033
1034 /* Return the number of characters in the NBYTES bytes at PTR.
1035 This works by looking at the contents and checking for multibyte sequences.
1036 It ignores enable-multibyte-characters. */
1037
1038 int
1039 multibyte_chars_in_text (ptr, nbytes)
1040 unsigned char *ptr;
1041 int nbytes;
1042 {
1043 unsigned char *endp;
1044 int chars;
1045
1046 endp = ptr + nbytes;
1047 chars = 0;
1048
1049 while (ptr < endp)
1050 {
1051 if (*ptr == LEADING_CODE_COMPOSITION)
1052 {
1053 ptr++;
1054 while (ptr < endp && ! CHAR_HEAD_P (*ptr)) ptr++;
1055 }
1056 else
1057 ptr += BYTES_BY_CHAR_HEAD (*ptr);
1058 chars++;
1059 }
1060
1061 return chars;
1062 }
1063
1064 DEFUN ("string", Fstring, Sstring, 1, MANY, 0,
1065 "Concatenate all the argument characters and make the result a string.")
1066 (n, args)
1067 int n;
1068 Lisp_Object *args;
1069 {
1070 int i;
1071 unsigned char *buf
1072 = (unsigned char *) alloca (MAX_LENGTH_OF_MULTI_BYTE_FORM * n);
1073 unsigned char *p = buf;
1074 Lisp_Object val;
1075
1076 for (i = 0; i < n; i++)
1077 {
1078 int c, len;
1079 unsigned char *str;
1080
1081 if (!INTEGERP (args[i]))
1082 {
1083 free (buf);
1084 CHECK_NUMBER (args[i], 0);
1085 }
1086 c = XINT (args[i]);
1087 len = CHAR_STRING (c, p, str);
1088 if (p != str)
1089 /* C is a composite character. */
1090 bcopy (str, p, len);
1091 p += len;
1092 }
1093
1094 val = make_multibyte_string (buf, n, p - buf);
1095 return val;
1096 }
1097
1098 #endif /* emacs */
1099 \f
1100 /*** Composite characters staffs ***/
1101
1102 /* Each composite character is identified by CMPCHAR-ID which is
1103 assigned when Emacs needs the character code of the composite
1104 character (e.g. when displaying it on the screen). See the
1105 document "GENERAL NOTE on COMPOSITE CHARACTER" in `charset.h' how a
1106 composite character is represented in Emacs. */
1107
1108 /* If `static' is defined, it means that it is defined to null string. */
1109 #ifndef static
1110 /* The following function is copied from lread.c. */
1111 static int
1112 hash_string (ptr, len)
1113 unsigned char *ptr;
1114 int len;
1115 {
1116 register unsigned char *p = ptr;
1117 register unsigned char *end = p + len;
1118 register unsigned char c;
1119 register int hash = 0;
1120
1121 while (p != end)
1122 {
1123 c = *p++;
1124 if (c >= 0140) c -= 40;
1125 hash = ((hash<<3) + (hash>>28) + c);
1126 }
1127 return hash & 07777777777;
1128 }
1129 #endif
1130
1131 #define CMPCHAR_HASH_TABLE_SIZE 0xFFF
1132
1133 static int *cmpchar_hash_table[CMPCHAR_HASH_TABLE_SIZE];
1134
1135 /* Each element of `cmpchar_hash_table' is a pointer to an array of
1136 integer, where the 1st element is the size of the array, the 2nd
1137 element is how many elements are actually used in the array, and
1138 the remaining elements are CMPCHAR-IDs of composite characters of
1139 the same hash value. */
1140 #define CMPCHAR_HASH_SIZE(table) table[0]
1141 #define CMPCHAR_HASH_USED(table) table[1]
1142 #define CMPCHAR_HASH_CMPCHAR_ID(table, i) table[i]
1143
1144 /* Return CMPCHAR-ID of the composite character in STR of the length
1145 LEN. If the composite character has not yet been registered,
1146 register it in `cmpchar_table' and assign new CMPCHAR-ID. This
1147 is the sole function for assigning CMPCHAR-ID. */
1148 int
1149 str_cmpchar_id (str, len)
1150 unsigned char *str;
1151 int len;
1152 {
1153 int hash_idx, *hashp;
1154 unsigned char *buf;
1155 int embedded_rule; /* 1 if composition rule is embedded. */
1156 int chars; /* number of components. */
1157 int i;
1158 struct cmpchar_info *cmpcharp;
1159
1160 if (len < 5)
1161 /* Any composite char have at least 3-byte length. */
1162 return -1;
1163
1164 /* The second byte 0xFF means compostion rule is embedded. */
1165 embedded_rule = (str[1] == 0xFF);
1166
1167 /* At first, get the actual length of the composite character. */
1168 {
1169 unsigned char *p, *endp = str + 1, *lastp = str + len;
1170 int bytes;
1171
1172 while (endp < lastp && ! CHAR_HEAD_P (*endp)) endp++;
1173 chars = 0;
1174 p = str + 1 + embedded_rule;
1175 while (p < endp)
1176 {
1177 /* No need of checking if *P is 0xA0 because
1178 BYTES_BY_CHAR_HEAD (0x80) surely returns 2. */
1179 p += (bytes = BYTES_BY_CHAR_HEAD (*p - 0x20) + embedded_rule);
1180 chars++;
1181 }
1182 len = (p -= embedded_rule) - str;
1183 if (p > endp)
1184 len -= - bytes, chars--;
1185
1186 if (chars < 2 || chars > MAX_COMPONENT_COUNT)
1187 /* Invalid number of components. */
1188 return -1;
1189 }
1190 hash_idx = hash_string (str, len) % CMPCHAR_HASH_TABLE_SIZE;
1191 hashp = cmpchar_hash_table[hash_idx];
1192
1193 /* Then, look into the hash table. */
1194 if (hashp != NULL)
1195 /* Find the correct one among composite characters of the same
1196 hash value. */
1197 for (i = 2; i < CMPCHAR_HASH_USED (hashp); i++)
1198 {
1199 cmpcharp = cmpchar_table[CMPCHAR_HASH_CMPCHAR_ID (hashp, i)];
1200 if (len == cmpcharp->len
1201 && ! bcmp (str, cmpcharp->data, len))
1202 return CMPCHAR_HASH_CMPCHAR_ID (hashp, i);
1203 }
1204
1205 /* We have to register the composite character in cmpchar_table. */
1206 if (n_cmpchars > (CHAR_FIELD2_MASK | CHAR_FIELD3_MASK))
1207 /* No, we have no more room for a new composite character. */
1208 return -1;
1209
1210 /* Make the entry in hash table. */
1211 if (hashp == NULL)
1212 {
1213 /* Make a table for 8 composite characters initially. */
1214 hashp = (cmpchar_hash_table[hash_idx]
1215 = (int *) xmalloc (sizeof (int) * (2 + 8)));
1216 CMPCHAR_HASH_SIZE (hashp) = 10;
1217 CMPCHAR_HASH_USED (hashp) = 2;
1218 }
1219 else if (CMPCHAR_HASH_USED (hashp) >= CMPCHAR_HASH_SIZE (hashp))
1220 {
1221 CMPCHAR_HASH_SIZE (hashp) += 8;
1222 hashp = (cmpchar_hash_table[hash_idx]
1223 = (int *) xrealloc (hashp,
1224 sizeof (int) * CMPCHAR_HASH_SIZE (hashp)));
1225 }
1226 CMPCHAR_HASH_CMPCHAR_ID (hashp, CMPCHAR_HASH_USED (hashp)) = n_cmpchars;
1227 CMPCHAR_HASH_USED (hashp)++;
1228
1229 /* Set information of the composite character in cmpchar_table. */
1230 if (cmpchar_table_size == 0)
1231 {
1232 /* This is the first composite character to be registered. */
1233 cmpchar_table_size = 256;
1234 cmpchar_table
1235 = (struct cmpchar_info **) xmalloc (sizeof (cmpchar_table[0])
1236 * cmpchar_table_size);
1237 }
1238 else if (cmpchar_table_size <= n_cmpchars)
1239 {
1240 cmpchar_table_size += 256;
1241 cmpchar_table
1242 = (struct cmpchar_info **) xrealloc (cmpchar_table,
1243 sizeof (cmpchar_table[0])
1244 * cmpchar_table_size);
1245 }
1246
1247 cmpcharp = (struct cmpchar_info *) xmalloc (sizeof (struct cmpchar_info));
1248
1249 cmpcharp->len = len;
1250 cmpcharp->data = (unsigned char *) xmalloc (len + 1);
1251 bcopy (str, cmpcharp->data, len);
1252 cmpcharp->data[len] = 0;
1253 cmpcharp->glyph_len = chars;
1254 cmpcharp->glyph = (GLYPH *) xmalloc (sizeof (GLYPH) * chars);
1255 if (embedded_rule)
1256 {
1257 cmpcharp->cmp_rule = (unsigned char *) xmalloc (chars);
1258 cmpcharp->col_offset = (float *) xmalloc (sizeof (float) * chars);
1259 }
1260 else
1261 {
1262 cmpcharp->cmp_rule = NULL;
1263 cmpcharp->col_offset = NULL;
1264 }
1265
1266 /* Setup GLYPH data and composition rules (if any) so as not to make
1267 them every time on displaying. */
1268 {
1269 unsigned char *bufp;
1270 int width;
1271 float leftmost = 0.0, rightmost = 1.0;
1272
1273 if (embedded_rule)
1274 /* At first, col_offset[N] is set to relative to col_offset[0]. */
1275 cmpcharp->col_offset[0] = 0;
1276
1277 for (i = 0, bufp = cmpcharp->data + 1; i < chars; i++)
1278 {
1279 if (embedded_rule)
1280 cmpcharp->cmp_rule[i] = *bufp++;
1281
1282 if (*bufp == 0xA0) /* This is an ASCII character. */
1283 {
1284 cmpcharp->glyph[i] = FAST_MAKE_GLYPH ((*++bufp & 0x7F), 0);
1285 width = 1;
1286 bufp++;
1287 }
1288 else /* Multibyte character. */
1289 {
1290 /* Make `bufp' point normal multi-byte form temporally. */
1291 *bufp -= 0x20;
1292 cmpcharp->glyph[i]
1293 = FAST_MAKE_GLYPH (string_to_non_ascii_char (bufp, 4, 0), 0);
1294 width = WIDTH_BY_CHAR_HEAD (*bufp);
1295 *bufp += 0x20;
1296 bufp += BYTES_BY_CHAR_HEAD (*bufp - 0x20);
1297 }
1298
1299 if (embedded_rule && i > 0)
1300 {
1301 /* Reference points (global_ref and new_ref) are
1302 encoded as below:
1303
1304 0--1--2 -- ascent
1305 | |
1306 | |
1307 | 4 -+--- center
1308 -- 3 5 -- baseline
1309 | |
1310 6--7--8 -- descent
1311
1312 Now, we calculate the column offset of the new glyph
1313 from the left edge of the first glyph. This can avoid
1314 the same calculation everytime displaying this
1315 composite character. */
1316
1317 /* Reference points of global glyph and new glyph. */
1318 int global_ref = (cmpcharp->cmp_rule[i] - 0xA0) / 9;
1319 int new_ref = (cmpcharp->cmp_rule[i] - 0xA0) % 9;
1320 /* Column offset relative to the first glyph. */
1321 float left = (leftmost
1322 + (global_ref % 3) * (rightmost - leftmost) / 2.0
1323 - (new_ref % 3) * width / 2.0);
1324
1325 cmpcharp->col_offset[i] = left;
1326 if (left < leftmost)
1327 leftmost = left;
1328 if (left + width > rightmost)
1329 rightmost = left + width;
1330 }
1331 else
1332 {
1333 if (width > rightmost)
1334 rightmost = width;
1335 }
1336 }
1337 if (embedded_rule)
1338 {
1339 /* Now col_offset[N] are relative to the left edge of the
1340 first component. Make them relative to the left edge of
1341 overall glyph. */
1342 for (i = 0; i < chars; i++)
1343 cmpcharp->col_offset[i] -= leftmost;
1344 /* Make rightmost holds width of overall glyph. */
1345 rightmost -= leftmost;
1346 }
1347
1348 cmpcharp->width = rightmost;
1349 if (cmpcharp->width < rightmost)
1350 /* To get a ceiling integer value. */
1351 cmpcharp->width++;
1352 }
1353
1354 cmpchar_table[n_cmpchars] = cmpcharp;
1355
1356 return n_cmpchars++;
1357 }
1358
1359 /* Return the Nth element of the composite character C. */
1360 int
1361 cmpchar_component (c, n)
1362 unsigned int c, n;
1363 {
1364 int id = COMPOSITE_CHAR_ID (c);
1365
1366 if (id >= n_cmpchars /* C is not a valid composite character. */
1367 || n >= cmpchar_table[id]->glyph_len) /* No such component. */
1368 return -1;
1369 /* No face data is stored in glyph code. */
1370 return ((int) (cmpchar_table[id]->glyph[n]));
1371 }
1372
1373 DEFUN ("cmpcharp", Fcmpcharp, Scmpcharp, 1, 1, 0,
1374 "T if CHAR is a composite character.")
1375 (ch)
1376 Lisp_Object ch;
1377 {
1378 CHECK_NUMBER (ch, 0);
1379 return (COMPOSITE_CHAR_P (XINT (ch)) ? Qt : Qnil);
1380 }
1381
1382 DEFUN ("composite-char-component", Fcmpchar_component, Scmpchar_component,
1383 2, 2, 0,
1384 "Return the IDXth component character of composite character CHARACTER.")
1385 (character, idx)
1386 Lisp_Object character, idx;
1387 {
1388 int c;
1389
1390 CHECK_NUMBER (character, 0);
1391 CHECK_NUMBER (idx, 1);
1392
1393 if ((c = cmpchar_component (XINT (character), XINT (idx))) < 0)
1394 args_out_of_range (character, idx);
1395
1396 return make_number (c);
1397 }
1398
1399 DEFUN ("composite-char-composition-rule", Fcmpchar_cmp_rule, Scmpchar_cmp_rule,
1400 2, 2, 0,
1401 "Return the Nth composition rule embedded in composite character CHARACTER.\n\
1402 The returned rule is for composing the Nth component\n\
1403 on the (N-1)th component. If N is 0, the returned value is always 255.")
1404 (character, n)
1405 Lisp_Object character, n;
1406 {
1407 int id, i;
1408
1409 CHECK_NUMBER (character, 0);
1410 CHECK_NUMBER (n, 1);
1411
1412 id = COMPOSITE_CHAR_ID (XINT (character));
1413 if (id < 0 || id >= n_cmpchars)
1414 error ("Invalid composite character: %d", XINT (character));
1415 i = XINT (n);
1416 if (i > cmpchar_table[id]->glyph_len)
1417 args_out_of_range (character, n);
1418
1419 return make_number (cmpchar_table[id]->cmp_rule[i]);
1420 }
1421
1422 DEFUN ("composite-char-composition-rule-p", Fcmpchar_cmp_rule_p,
1423 Scmpchar_cmp_rule_p, 1, 1, 0,
1424 "Return non-nil if composite character CHARACTER contains a embedded rule.")
1425 (character)
1426 Lisp_Object character;
1427 {
1428 int id;
1429
1430 CHECK_NUMBER (character, 0);
1431 id = COMPOSITE_CHAR_ID (XINT (character));
1432 if (id < 0 || id >= n_cmpchars)
1433 error ("Invalid composite character: %d", XINT (character));
1434
1435 return (cmpchar_table[id]->cmp_rule ? Qt : Qnil);
1436 }
1437
1438 DEFUN ("composite-char-component-count", Fcmpchar_cmp_count,
1439 Scmpchar_cmp_count, 1, 1, 0,
1440 "Return number of compoents of composite character CHARACTER.")
1441 (character)
1442 Lisp_Object character;
1443 {
1444 int id;
1445
1446 CHECK_NUMBER (character, 0);
1447 id = COMPOSITE_CHAR_ID (XINT (character));
1448 if (id < 0 || id >= n_cmpchars)
1449 error ("Invalid composite character: %d", XINT (character));
1450
1451 return (make_number (cmpchar_table[id]->glyph_len));
1452 }
1453
1454 DEFUN ("compose-string", Fcompose_string, Scompose_string,
1455 1, 1, 0,
1456 "Return one char string composed from all characters in STRING.")
1457 (str)
1458 Lisp_Object str;
1459 {
1460 unsigned char buf[MAX_LENGTH_OF_MULTI_BYTE_FORM], *p, *pend, *ptemp;
1461 int len, i;
1462
1463 CHECK_STRING (str, 0);
1464
1465 buf[0] = LEADING_CODE_COMPOSITION;
1466 p = XSTRING (str)->data;
1467 pend = p + XSTRING (str)->size_byte;
1468 i = 1;
1469 while (p < pend)
1470 {
1471 if (*p < 0x20 || *p == 127) /* control code */
1472 error ("Invalid component character: %d", *p);
1473 else if (*p < 0x80) /* ASCII */
1474 {
1475 if (i + 2 >= MAX_LENGTH_OF_MULTI_BYTE_FORM)
1476 error ("Too long string to be composed: %s", XSTRING (str)->data);
1477 /* Prepend an ASCII charset indicator 0xA0, set MSB of the
1478 code itself. */
1479 buf[i++] = 0xA0;
1480 buf[i++] = *p++ + 0x80;
1481 }
1482 else if (*p == LEADING_CODE_COMPOSITION) /* composite char */
1483 {
1484 /* Already composed. Eliminate the heading
1485 LEADING_CODE_COMPOSITION, keep the remaining bytes
1486 unchanged. */
1487 p++;
1488 ptemp = p;
1489 while (! CHAR_HEAD_P (*p)) p++;
1490 if (i + (p - ptemp) >= MAX_LENGTH_OF_MULTI_BYTE_FORM)
1491 error ("Too long string to be composed: %s", XSTRING (str)->data);
1492 bcopy (ptemp, buf + i, p - ptemp);
1493 i += p - ptemp;
1494 }
1495 else /* multibyte char */
1496 {
1497 /* Add 0x20 to the base leading-code, keep the remaining
1498 bytes unchanged. */
1499 len = BYTES_BY_CHAR_HEAD (*p);
1500 if (i + len >= MAX_LENGTH_OF_MULTI_BYTE_FORM)
1501 error ("Too long string to be composed: %s", XSTRING (str)->data);
1502 bcopy (p, buf + i, len);
1503 buf[i] += 0x20;
1504 p += len, i += len;
1505 }
1506 }
1507
1508 if (i < 5)
1509 /* STR contains only one character, which can't be composed. */
1510 error ("Too short string to be composed: %s", XSTRING (str)->data);
1511
1512 return make_multibyte_string (buf, 1, i);
1513 }
1514
1515 \f
1516 charset_id_internal (charset_name)
1517 char *charset_name;
1518 {
1519 Lisp_Object val = Fget (intern (charset_name), Qcharset);
1520
1521 if (!VECTORP (val))
1522 error ("Charset %s is not defined", charset_name);
1523
1524 return (XINT (XVECTOR (val)->contents[0]));
1525 }
1526
1527 DEFUN ("setup-special-charsets", Fsetup_special_charsets,
1528 Ssetup_special_charsets, 0, 0, 0, "Internal use only.")
1529 ()
1530 {
1531 charset_latin_iso8859_1 = charset_id_internal ("latin-iso8859-1");
1532 charset_jisx0208_1978 = charset_id_internal ("japanese-jisx0208-1978");
1533 charset_jisx0208 = charset_id_internal ("japanese-jisx0208");
1534 charset_katakana_jisx0201 = charset_id_internal ("katakana-jisx0201");
1535 charset_latin_jisx0201 = charset_id_internal ("latin-jisx0201");
1536 charset_big5_1 = charset_id_internal ("chinese-big5-1");
1537 charset_big5_2 = charset_id_internal ("chinese-big5-2");
1538 return Qnil;
1539 }
1540
1541 init_charset_once ()
1542 {
1543 int i, j, k;
1544
1545 staticpro (&Vcharset_table);
1546 staticpro (&Vcharset_symbol_table);
1547
1548 /* This has to be done here, before we call Fmake_char_table. */
1549 Qcharset_table = intern ("charset-table");
1550 staticpro (&Qcharset_table);
1551
1552 /* Intern this now in case it isn't already done.
1553 Setting this variable twice is harmless.
1554 But don't staticpro it here--that is done in alloc.c. */
1555 Qchar_table_extra_slots = intern ("char-table-extra-slots");
1556
1557 /* Now we are ready to set up this property, so we can
1558 create the charset table. */
1559 Fput (Qcharset_table, Qchar_table_extra_slots, make_number (0));
1560 Vcharset_table = Fmake_char_table (Qcharset_table, Qnil);
1561
1562 Vcharset_symbol_table = Fmake_vector (make_number (MAX_CHARSET + 1), Qnil);
1563
1564 /* Setup tables. */
1565 for (i = 0; i < 2; i++)
1566 for (j = 0; j < 2; j++)
1567 for (k = 0; k < 128; k++)
1568 iso_charset_table [i][j][k] = -1;
1569
1570 bzero (cmpchar_hash_table, sizeof cmpchar_hash_table);
1571 cmpchar_table_size = n_cmpchars = 0;
1572
1573 for (i = 0; i < 256; i++)
1574 BYTES_BY_CHAR_HEAD (i) = 1;
1575 BYTES_BY_CHAR_HEAD (LEADING_CODE_PRIVATE_11) = 3;
1576 BYTES_BY_CHAR_HEAD (LEADING_CODE_PRIVATE_12) = 3;
1577 BYTES_BY_CHAR_HEAD (LEADING_CODE_PRIVATE_21) = 4;
1578 BYTES_BY_CHAR_HEAD (LEADING_CODE_PRIVATE_22) = 4;
1579 /* The following doesn't reflect the actual bytes, but just to tell
1580 that it is a start of a multibyte character. */
1581 BYTES_BY_CHAR_HEAD (LEADING_CODE_COMPOSITION) = 2;
1582
1583 for (i = 0; i < 128; i++)
1584 WIDTH_BY_CHAR_HEAD (i) = 1;
1585 for (; i < 256; i++)
1586 WIDTH_BY_CHAR_HEAD (i) = 4;
1587 WIDTH_BY_CHAR_HEAD (LEADING_CODE_PRIVATE_11) = 1;
1588 WIDTH_BY_CHAR_HEAD (LEADING_CODE_PRIVATE_12) = 2;
1589 WIDTH_BY_CHAR_HEAD (LEADING_CODE_PRIVATE_21) = 1;
1590 WIDTH_BY_CHAR_HEAD (LEADING_CODE_PRIVATE_22) = 2;
1591 }
1592
1593 #ifdef emacs
1594
1595 syms_of_charset ()
1596 {
1597 Qascii = intern ("ascii");
1598 staticpro (&Qascii);
1599
1600 Qcharset = intern ("charset");
1601 staticpro (&Qcharset);
1602
1603 /* Define ASCII charset now. */
1604 update_charset_table (make_number (CHARSET_ASCII),
1605 make_number (1), make_number (94),
1606 make_number (1),
1607 make_number (0),
1608 make_number ('B'),
1609 make_number (0),
1610 build_string ("ASCII"),
1611 build_string ("ASCII"),
1612 build_string ("ASCII (ISO646 IRV)"));
1613 CHARSET_SYMBOL (CHARSET_ASCII) = Qascii;
1614 Fput (Qascii, Qcharset, CHARSET_TABLE_ENTRY (CHARSET_ASCII));
1615
1616 Qcomposition = intern ("composition");
1617 staticpro (&Qcomposition);
1618 CHARSET_SYMBOL (CHARSET_COMPOSITION) = Qcomposition;
1619
1620 defsubr (&Sdefine_charset);
1621 defsubr (&Sget_unused_iso_final_char);
1622 defsubr (&Sdeclare_equiv_charset);
1623 defsubr (&Sfind_charset_region);
1624 defsubr (&Sfind_charset_string);
1625 defsubr (&Smake_char_internal);
1626 defsubr (&Ssplit_char);
1627 defsubr (&Schar_charset);
1628 defsubr (&Siso_charset);
1629 defsubr (&Schar_valid_p);
1630 defsubr (&Schar_bytes);
1631 defsubr (&Schar_width);
1632 defsubr (&Sstring_width);
1633 defsubr (&Schar_direction);
1634 defsubr (&Schars_in_region);
1635 defsubr (&Sstring);
1636 defsubr (&Scmpcharp);
1637 defsubr (&Scmpchar_component);
1638 defsubr (&Scmpchar_cmp_rule);
1639 defsubr (&Scmpchar_cmp_rule_p);
1640 defsubr (&Scmpchar_cmp_count);
1641 defsubr (&Scompose_string);
1642 defsubr (&Ssetup_special_charsets);
1643
1644 DEFVAR_LISP ("charset-list", &Vcharset_list,
1645 "List of charsets ever defined.");
1646 Vcharset_list = Fcons (Qascii, Qnil);
1647
1648 DEFVAR_INT ("leading-code-composition", &leading_code_composition,
1649 "Leading-code of composite characters.");
1650 leading_code_composition = LEADING_CODE_COMPOSITION;
1651
1652 DEFVAR_INT ("leading-code-private-11", &leading_code_private_11,
1653 "Leading-code of private TYPE9N charset of column-width 1.");
1654 leading_code_private_11 = LEADING_CODE_PRIVATE_11;
1655
1656 DEFVAR_INT ("leading-code-private-12", &leading_code_private_12,
1657 "Leading-code of private TYPE9N charset of column-width 2.");
1658 leading_code_private_12 = LEADING_CODE_PRIVATE_12;
1659
1660 DEFVAR_INT ("leading-code-private-21", &leading_code_private_21,
1661 "Leading-code of private TYPE9Nx9N charset of column-width 1.");
1662 leading_code_private_21 = LEADING_CODE_PRIVATE_21;
1663
1664 DEFVAR_INT ("leading-code-private-22", &leading_code_private_22,
1665 "Leading-code of private TYPE9Nx9N charset of column-width 2.");
1666 leading_code_private_22 = LEADING_CODE_PRIVATE_22;
1667 }
1668
1669 #endif /* emacs */