(fix_submap_inheritance, get_keyelt, store_in_keymap,
[bpt/emacs.git] / src / charset.h
CommitLineData
4ed46869 1/* Header for multilingual character handler.
4a2f9c6a 2 Copyright (C) 1995, 1997, 1998 Electrotechnical Laboratory, JAPAN.
75c8c592 3 Licensed to the Free Software Foundation.
4ed46869 4
369314dc
KH
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
4ed46869 11
369314dc
KH
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
4ed46869 16
369314dc
KH
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
4ed46869
KH
21
22#ifndef _CHARSET_H
23#define _CHARSET_H
24
25/*** GENERAL NOTE on CHARACTER SET (CHARSET) ***
26
27 A character set ("charset" hereafter) is a meaningful collection
28 (i.e. language, culture, functionality, etc) of characters. Emacs
29 handles multiple charsets at once. Each charset corresponds to one
30 of ISO charsets (except for a special charset for composition
31 characters). Emacs identifies a charset by a unique identification
32 number, whereas ISO identifies a charset by a triplet of DIMENSION,
33 CHARS and FINAL-CHAR. So, hereafter, just saying "charset" means an
34 identification number (integer value).
35
36 The value range of charset is 0x00, 0x80..0xFE. There are four
37 kinds of charset depending on DIMENSION (1 or 2) and CHARS (94 or
38 96). For instance, a charset of DIMENSION2_CHARS94 contains 94x94
39
40
41 Within Emacs Lisp, a charset is treated as a symbol which has a
42 property `charset'. The property value is a vector containing
43 various information about the charset. For readability of C codes,
44 we use the following convention on C variable names:
45 charset_symbol: Emacs Lisp symbol of a charset
46 charset_id: Emacs Lisp integer of an identification number of a charset
47 charset: C integer of an identification number of a charset
48
49 Each charset (except for ASCII) is assigned a base leading-code
50 (range 0x80..0x9D). In addition, a charset of greater than 0xA0
51 (whose base leading-code is 0x9A..0x9D) is assigned an extended
52 leading-code (range 0xA0..0xFE). In this case, each base
53 leading-code specify the allowable range of extended leading-code as
54 shown in the table below. A leading-code is used to represent a
55 character in Emacs' buffer and string.
56
57 We call a charset which has extended leading-code as "private
58 charset" because those are mainly for a charset which is not
59 registered by ISO. On the contrary, we call a charset which does
60 not have extended leading-code as "official charset".
61
62 ---------------------------------------------------------------------------
63 charset dimension base leading-code extended leading-code
64 ---------------------------------------------------------------------------
65 0x00 official dim1 -- none -- -- none --
66 (ASCII)
67 0x01..0x7F --never used--
68 0x80 COMPOSITION same as charset -- none --
69 0x81..0x8F official dim1 same as charset -- none --
70 0x90..0x99 official dim2 same as charset -- none --
71 0x9A..0x9F --never used--
72 0xA0..0xDF private dim1 0x9A same as charset
73 of 1-column width
74 0xE0..0xEF private dim1 0x9B same as charset
75 of 2-column width
76 0xF0..0xF4 private dim2 0x9C same as charset
77 of 1-column width
78 0xF5..0xFE private dim2 0x9D same as charset
79 of 2-column width
80 0xFF --never used--
81 ---------------------------------------------------------------------------
82
83 In the table, "COMPOSITION" means a charset for a composite
84 character which is a character composed from several (up to 16)
85 non-composite characters (components). Although a composite
86 character can contain components of many charsets, a composite
87 character itself belongs to the charset CHARSET-COMPOSITION. See
88 the document "GENERAL NOTE on COMPOSITE CHARACTER" below for more
89 detail.
90
91*/
92
93/* Definition of special leading-codes. */
94/* Base leading-code. */
95/* Special leading-code followed by components of a composite character. */
96#define LEADING_CODE_COMPOSITION 0x80
97/* Leading-code followed by extended leading-code. */
98#define LEADING_CODE_PRIVATE_11 0x9A /* for private DIMENSION1 of 1-column */
99#define LEADING_CODE_PRIVATE_12 0x9B /* for private DIMENSION1 of 2-column */
100#define LEADING_CODE_PRIVATE_21 0x9C /* for private DIMENSION2 of 1-column */
5c6257e5 101#define LEADING_CODE_PRIVATE_22 0x9D /* for private DIMENSION2 of 2-column */
4ed46869
KH
102
103/* Extended leading-code. */
104/* Start of each extended leading-codes. */
105#define LEADING_CODE_EXT_11 0xA0 /* follows LEADING_CODE_PRIVATE_11 */
106#define LEADING_CODE_EXT_12 0xE0 /* follows LEADING_CODE_PRIVATE_12 */
107#define LEADING_CODE_EXT_21 0xF0 /* follows LEADING_CODE_PRIVATE_21 */
108#define LEADING_CODE_EXT_22 0xF5 /* follows LEADING_CODE_PRIVATE_22 */
109/* Maximum value of extended leading-codes. */
110#define LEADING_CODE_EXT_MAX 0xFE
111
112/* Definition of minimum/maximum charset of each DIMENSION. */
113#define MIN_CHARSET_OFFICIAL_DIMENSION1 0x81
114#define MAX_CHARSET_OFFICIAL_DIMENSION1 0x8F
115#define MIN_CHARSET_OFFICIAL_DIMENSION2 0x90
116#define MAX_CHARSET_OFFICIAL_DIMENSION2 0x99
117#define MIN_CHARSET_PRIVATE_DIMENSION1 LEADING_CODE_EXT_11
118#define MIN_CHARSET_PRIVATE_DIMENSION2 LEADING_CODE_EXT_21
119
03e66c53
KH
120/* Maximum value of overall charset identification number. */
121#define MAX_CHARSET 0xFE
122
4ed46869
KH
123/* Definition of special charsets. */
124#define CHARSET_ASCII 0
125#define CHARSET_COMPOSITION 0x80
126
127extern int charset_ascii; /* ASCII */
128extern int charset_composition; /* for a composite character */
129extern int charset_latin_iso8859_1; /* ISO8859-1 (Latin-1) */
130extern int charset_jisx0208_1978; /* JISX0208.1978 (Japanese Kanji old set) */
131extern int charset_jisx0208; /* JISX0208.1983 (Japanese Kanji) */
132extern int charset_katakana_jisx0201; /* JISX0201.Kana (Japanese Katakana) */
133extern int charset_latin_jisx0201; /* JISX0201.Roman (Japanese Roman) */
134extern int charset_big5_1; /* Big5 Level 1 (Chinese Traditional) */
135extern int charset_big5_2; /* Big5 Level 2 (Chinese Traditional) */
136
c399b461
RS
137/* Check if CH is the head of multi-byte form, i.e.,
138 an ASCII character or a base leading-code. */
139#define CHAR_HEAD_P(ch) ((unsigned char) (ch) < 0xA0)
4ed46869
KH
140
141/*** GENERAL NOTE on CHARACTER REPRESENTATION ***
142
143 At first, the term "character" or "char" is used for a multilingual
144 character (of course, including ASCII character), not for a byte in
145 computer memory. We use the term "code" or "byte" for the latter
146 case.
147
148 A character is identified by charset and one or two POSITION-CODEs.
149 POSITION-CODE is the position of the character in the charset. A
150 character of DIMENSION1 charset has one POSITION-CODE: POSITION-CODE-1.
151 A character of DIMENSION2 charset has two POSITION-CODE:
152 POSITION-CODE-1 and POSITION-CODE-2. The code range of
153 POSITION-CODE is 0x20..0x7F.
154
155 Emacs has two kinds of representation of a character: multi-byte
156 form (for buffer and string) and single-word form (for character
157 object in Emacs Lisp). The latter is called "character code" here
158 after. Both representation encode the information of charset and
159 POSITION-CODE but in a different way (for instance, MSB of
160 POSITION-CODE is set in multi-byte form).
161
162 For details of multi-byte form, see the section "2. Emacs internal
163 format handlers" of `coding.c'.
164
165 Emacs uses 19 bits for a character code. The bits are divided into
166 3 fields: FIELD1(5bits):FIELD2(7bits):FIELD3(7bits).
167
168 A character code of DIMENSION1 character uses FIELD2 to hold charset
169 and FIELD3 to hold POSITION-CODE-1. A character code of DIMENSION2
170 character uses FIELD1 to hold charset, FIELD2 and FIELD3 to hold
171 POSITION-CODE-1 and POSITION-CODE-2 respectively.
172
173 More precisely...
174
175 FIELD2 of DIMENSION1 character (except for ASCII) is "charset - 0x70".
176 This is to make all character codes except for ASCII greater than
177 256 (ASCII's FIELD2 is 0). So, the range of FIELD2 of DIMENSION1
178 character is 0 or 0x11..0x7F.
179
180 FIELD1 of DIMENSION2 character is "charset - 0x8F" for official
181 charset and "charset - 0xE0" for private charset. So, the range of
182 FIELD1 of DIMENSION2 character is 0x01..0x1E.
183
184 -----------------------------------------------------------------------
185 charset FIELD1 (5-bit) FIELD2 (7-bit) FIELD3 (7-bit)
186 -----------------------------------------------------------------------
187 ASCII 0 0 POSITION-CODE-1
188 DIMENSION1 0 charset - 0x70 POSITION-CODE-1
189 DIMENSION2(o) charset - 0x8F POSITION-CODE-1 POSITION-CODE-2
190 DIMENSION2(p) charset - 0xE0 POSITION-CODE-1 POSITION-CODE-2
191 -----------------------------------------------------------------------
192 "(o)": official, "(p)": private
193 -----------------------------------------------------------------------
194
195*/
196
197/*** GENERAL NOTE on COMPOSITE CHARACTER ***
198
199 A composite character is a character composed from several (up to
200 16) non-composite characters (components). Although each components
201 can belong to any charset, a composite character itself belongs to
202 the charset `charset-composition' and is assigned a special
203 leading-code `LEADING_CODE_COMPOSITION' for multi-byte form. See
204 the document "2. Emacs internal format handlers" in `coding.c' for
205 more detail about multi-byte form.
206
207 A character code of composite character has special format. In the
208 above document, FIELD1 of a composite character is 0x1F. Each
209 composite character is assigned a sequential number CMPCHAR-ID.
210 FIELD2 and FIELD3 are combined to make 14bits field for holding
211 CMPCHAR-ID, which means that Emacs can handle at most 2^14 (= 16384)
212 composite characters at once.
213
214 -----------------------------------------------------------------------
215 charset FIELD1 (5-bit) FIELD2&3 (14-bit)
216 -----------------------------------------------------------------------
217 CHARSET-COMPOSITION 0x1F CMPCHAR-ID
218 -----------------------------------------------------------------------
219
220 Emacs assigns CMPCHAR-ID to a composite character only when it
221 requires the character code of the composite character (e.g. while
222 displaying the composite character).
223
224*/
225
226/* Masks of each field of character code. */
227#define CHAR_FIELD1_MASK (0x1F << 14)
228#define CHAR_FIELD2_MASK (0x7F << 7)
229#define CHAR_FIELD3_MASK 0x7F
230
231/* Macros to access each field of character C. */
232#define CHAR_FIELD1(c) (((c) & CHAR_FIELD1_MASK) >> 14)
233#define CHAR_FIELD2(c) (((c) & CHAR_FIELD2_MASK) >> 7)
234#define CHAR_FIELD3(c) ((c) & CHAR_FIELD3_MASK)
235
236/* Minimum character code of character of each DIMENSION. */
237#define MIN_CHAR_OFFICIAL_DIMENSION1 \
238 ((MIN_CHARSET_OFFICIAL_DIMENSION1 - 0x70) << 7)
239#define MIN_CHAR_PRIVATE_DIMENSION1 \
240 ((MIN_CHARSET_PRIVATE_DIMENSION1 - 0x70) << 7)
241#define MIN_CHAR_OFFICIAL_DIMENSION2 \
242 ((MIN_CHARSET_OFFICIAL_DIMENSION2 - 0x8F) << 14)
243#define MIN_CHAR_PRIVATE_DIMENSION2 \
244 ((MIN_CHARSET_PRIVATE_DIMENSION2 - 0xE0) << 14)
245#define MIN_CHAR_COMPOSITION \
246 (0x1F << 14)
c1f6608b 247#define MAX_CHAR_COMPOSITION GLYPH_MASK_CHAR
4ed46869
KH
248
249/* 1 if C is an ASCII character, else 0. */
250#define SINGLE_BYTE_CHAR_P(c) ((c) < 0x100)
251/* 1 if C is an composite character, else 0. */
252#define COMPOSITE_CHAR_P(c) ((c) >= MIN_CHAR_COMPOSITION)
253
7614f779
KH
254/* 1 if BYTE is a character in itself, in multibyte mode. */
255#define ASCII_BYTE_P(byte) ((byte) < 0x80)
256
4ed46869
KH
257/* A char-table containing information of each character set.
258
259 Unlike ordinary char-tables, this doesn't contain any nested table.
260 Only the top level elements are used. Each element is a vector of
261 the following information:
262 CHARSET-ID, BYTES, DIMENSION, CHARS, WIDTH, DIRECTION,
263 LEADING-CODE-BASE, LEADING-CODE-EXT,
264 ISO-FINAL-CHAR, ISO-GRAPHIC-PLANE,
265 REVERSE-CHARSET, SHORT-NAME, LONG-NAME, DESCRIPTION,
266 PLIST.
267
268 CHARSET-ID (integer) is the identification number of the charset.
269
270 BYTE (integer) is the length of multi-byte form of a character in
271 the charset: one of 1, 2, 3, and 4.
272
273 DIMENSION (integer) is the number of bytes to represent a character: 1 or 2.
274
275 CHARS (integer) is the number of characters in a dimension: 94 or 96.
276
277 WIDTH (integer) is the number of columns a character in the charset
278 occupies on the screen: one of 0, 1, and 2.
279
280 DIRECTION (integer) is the rendering direction of characters in the
281 charset when rendering. If 0, render from right to left, else
282 render from left to right.
283
284 LEADING-CODE-BASE (integer) is the base leading-code for the
285 charset.
286
287 LEADING-CODE-EXT (integer) is the extended leading-code for the
288 charset. All charsets of less than 0xA0 has the value 0.
289
290 ISO-FINAL-CHAR (character) is the final character of the
291 corresponding ISO 2022 charset.
292
293 ISO-GRAPHIC-PLANE (integer) is the graphic plane to be invoked
294 while encoding to variants of ISO 2022 coding system, one of the
295 following: 0/graphic-plane-left(GL), 1/graphic-plane-right(GR).
296
297 REVERSE-CHARSET (integer) is the charset which differs only in
298 LEFT-TO-RIGHT value from the charset. If there's no such a
299 charset, the value is -1.
300
301 SHORT-NAME (string) is the short name to refer to the charset.
302
303 LONG-NAME (string) is the long name to refer to the charset.
304
305 DESCRIPTION (string) is the description string of the charset.
306
307 PLIST (property list) may contain any type of information a user
308 want to put and get by functions `put-charset-property' and
309 `get-charset-property' respectively. */
310extern Lisp_Object Vcharset_table;
311
312/* Macros to access various information of CHARSET in Vcharset_table.
313 We provide these macros for efficiency. No range check of CHARSET. */
314
315/* Return entry of CHARSET (lisp integer) in Vcharset_table. */
d6f92c04
KH
316#define CHARSET_TABLE_ENTRY(charset) \
317 XCHAR_TABLE (Vcharset_table)->contents[((charset) == CHARSET_ASCII \
318 ? 0 : (charset) + 128)]
4ed46869
KH
319
320/* Return information INFO-IDX of CHARSET. */
321#define CHARSET_TABLE_INFO(charset, info_idx) \
322 XVECTOR (CHARSET_TABLE_ENTRY (charset))->contents[info_idx]
323
324#define CHARSET_ID_IDX (0)
325#define CHARSET_BYTES_IDX (1)
326#define CHARSET_DIMENSION_IDX (2)
327#define CHARSET_CHARS_IDX (3)
328#define CHARSET_WIDTH_IDX (4)
329#define CHARSET_DIRECTION_IDX (5)
330#define CHARSET_LEADING_CODE_BASE_IDX (6)
331#define CHARSET_LEADING_CODE_EXT_IDX (7)
332#define CHARSET_ISO_FINAL_CHAR_IDX (8)
333#define CHARSET_ISO_GRAPHIC_PLANE_IDX (9)
334#define CHARSET_REVERSE_CHARSET_IDX (10)
335#define CHARSET_SHORT_NAME_IDX (11)
336#define CHARSET_LONG_NAME_IDX (12)
337#define CHARSET_DESCRIPTION_IDX (13)
338#define CHARSET_PLIST_IDX (14)
339/* Size of a vector of each entry of Vcharset_table. */
340#define CHARSET_MAX_IDX (15)
341
342/* And several more macros to be used frequently. */
343#define CHARSET_BYTES(charset) \
344 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_BYTES_IDX))
345#define CHARSET_DIMENSION(charset) \
346 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_DIMENSION_IDX))
347#define CHARSET_CHARS(charset) \
348 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_CHARS_IDX))
349#define CHARSET_WIDTH(charset) \
350 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_WIDTH_IDX))
351#define CHARSET_DIRECTION(charset) \
352 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_DIRECTION_IDX))
353#define CHARSET_LEADING_CODE_BASE(charset) \
354 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_LEADING_CODE_BASE_IDX))
355#define CHARSET_LEADING_CODE_EXT(charset) \
356 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_LEADING_CODE_EXT_IDX))
357#define CHARSET_ISO_FINAL_CHAR(charset) \
358 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_ISO_FINAL_CHAR_IDX))
359#define CHARSET_ISO_GRAPHIC_PLANE(charset) \
360 XFASTINT (CHARSET_TABLE_INFO (charset, CHARSET_ISO_GRAPHIC_PLANE_IDX))
361#define CHARSET_REVERSE_CHARSET(charset) \
362 XINT (CHARSET_TABLE_INFO (charset, CHARSET_REVERSE_CHARSET_IDX))
363
364/* Macros to specify direction of a charset. */
365#define CHARSET_DIRECTION_LEFT_TO_RIGHT 0
366#define CHARSET_DIRECTION_RIGHT_TO_LEFT 1
367
368/* A vector of charset symbol indexed by charset-id. This is used
369 only for returning charset symbol from C functions. */
370extern Lisp_Object Vcharset_symbol_table;
371
372/* Return symbol of CHARSET. */
373#define CHARSET_SYMBOL(charset) \
374 XVECTOR (Vcharset_symbol_table)->contents[charset]
375
376/* 1 if CHARSET is valid, else 0. */
377#define CHARSET_VALID_P(charset) \
378 ((charset) == 0 \
379 || ((charset) >= 0x80 && (charset) <= MAX_CHARSET_OFFICIAL_DIMENSION2) \
03e66c53 380 || ((charset) >= MIN_CHARSET_PRIVATE_DIMENSION1 && (charset) <= MAX_CHARSET))
4ed46869
KH
381
382/* 1 if CHARSET is already defined, else 0. */
383#define CHARSET_DEFINED_P(charset) \
03e66c53 384 (((charset) >= 0) && ((charset) <= MAX_CHARSET) \
4ed46869
KH
385 && !NILP (CHARSET_TABLE_ENTRY (charset)))
386
387/* Since the information CHARSET-BYTES and CHARSET-WIDTH of
388 Vcharset_table can be retrieved only from the first byte of
389 multi-byte form (an ASCII code or a base leading-code), we provide
390 here tables to be used by macros BYTES_BY_CHAR_HEAD and
391 WIDTH_BY_CHAR_HEAD for faster information retrieval. */
392extern int bytes_by_char_head[256];
393extern int width_by_char_head[256];
394
395#define BYTES_BY_CHAR_HEAD(char_head) bytes_by_char_head[char_head]
396#define WIDTH_BY_CHAR_HEAD(char_head) width_by_char_head[char_head]
397
398/* Charset of the character C. */
399#define CHAR_CHARSET(c) \
400 (SINGLE_BYTE_CHAR_P (c) \
401 ? CHARSET_ASCII \
402 : ((c) < MIN_CHAR_OFFICIAL_DIMENSION2 \
403 ? CHAR_FIELD2 (c) + 0x70 \
404 : ((c) < MIN_CHAR_PRIVATE_DIMENSION2 \
405 ? CHAR_FIELD1 (c) + 0x8F \
406 : ((c) < MIN_CHAR_COMPOSITION \
407 ? CHAR_FIELD1 (c) + 0xE0 \
c1f6608b
KH
408 : ((c) <= MAX_CHAR_COMPOSITION \
409 ? CHARSET_COMPOSITION \
410 : CHARSET_ASCII)))))
4ed46869
KH
411
412/* Return charset at the place pointed by P. */
413#define CHARSET_AT(p) \
414 (*(p) < 0x80 \
415 ? CHARSET_ASCII \
416 : (*(p) == LEADING_CODE_COMPOSITION \
417 ? CHARSET_COMPOSITION \
418 : (*(p) < LEADING_CODE_PRIVATE_11 \
419 ? (int)*(p) \
420 : (*(p) <= LEADING_CODE_PRIVATE_22 \
421 ? (int)*((p) + 1) \
422 : -1))))
423
424/* Same as `CHARSET_AT ()' but perhaps runs faster because of an
425 additional argument C which is the code (byte) at P. */
426#define FIRST_CHARSET_AT(p, c) \
427 ((c) < 0x80 \
428 ? CHARSET_ASCII \
429 : ((c) == LEADING_CODE_COMPOSITION \
430 ? CHARSET_COMPOSITION \
431 : ((c) < LEADING_CODE_PRIVATE_11 \
432 ? (int)(c) \
433 : ((c) <= LEADING_CODE_PRIVATE_22 \
434 ? (int)*((p) + 1) \
435 : -1))))
436
437/* Check if two characters C1 and C2 belong to the same charset.
438 Always return 0 for composite characters. */
439#define SAME_CHARSET_P(c1, c2) \
440 (c1 < MIN_CHAR_COMPOSITION \
441 && (SINGLE_BYTE_CHAR_P (c1) \
442 ? SINGLE_BYTE_CHAR_P (c2) \
443 : (c1 < MIN_CHAR_OFFICIAL_DIMENSION2 \
444 ? (c1 & CHAR_FIELD2_MASK) == (c2 & CHAR_FIELD2_MASK) \
445 : (c1 & CHAR_FIELD1_MASK) == (c2 & CHAR_FIELD1_MASK))))
446
447/* Return a non-ASCII character of which charset is CHARSET and
448 position-codes are C1 and C2. DIMENSION1 character ignores C2. */
449#define MAKE_NON_ASCII_CHAR(charset, c1, c2) \
450 ((charset) == CHARSET_COMPOSITION \
451 ? MAKE_COMPOSITE_CHAR (((c1) << 7) + (c2)) \
452 : (CHARSET_DIMENSION (charset) == 1 \
453 ? (((charset) - 0x70) << 7) | (c1) \
454 : ((charset) < MIN_CHARSET_PRIVATE_DIMENSION2 \
455 ? (((charset) - 0x8F) << 14) | ((c1) << 7) | (c2) \
456 : (((charset) - 0xE0) << 14) | ((c1) << 7) | (c2))))
457
458/* Return a composite character of which CMPCHAR-ID is ID. */
459#define MAKE_COMPOSITE_CHAR(id) (MIN_CHAR_COMPOSITION + (id))
460
461/* Return CMPCHAR-ID of a composite character C. */
462#define COMPOSITE_CHAR_ID(c) ((c) - MIN_CHAR_COMPOSITION)
463
464/* Return a character of which charset is CHARSET and position-codes
465 are C1 and C2. DIMENSION1 character ignores C2. */
466#define MAKE_CHAR(charset, c1, c2) \
467 ((charset) == CHARSET_ASCII \
468 ? (c1) \
469 : MAKE_NON_ASCII_CHAR ((charset), (c1) & 0x7F, (c2) & 0x7F))
470
15979e9e
KH
471/* If GENERICP is nonzero, return nonzero iff C is a valid normal or
472 generic character. If GENERICP is zero, return nonzero iff C is a
473 valid normal character. */
474#define CHAR_VALID_P(c, genericp) \
475 ((c) >= 0 \
476 && (SINGLE_BYTE_CHAR_P (c) || char_valid_p (c, genericp)))
c6112b99 477
54e15bb9
KH
478/* This default value is used when nonascii-translate-table or
479 nonascii-insert-offset fail to convert unibyte character to a valid
480 multibyte character. This makes a Latin-1 character. */
481
482#define DEFAULT_NONASCII_INSERT_OFFSET 0x800
483
484/* Check if the character C is valid as a multibyte character. */
485
486#define VALID_MULTIBYTE_CHAR_P(c) \
487 ((c) < MIN_CHAR_OFFICIAL_DIMENSION2 \
488 ? (!NILP (XCHAR_TABLE (Vcharset_table)->contents[CHAR_FIELD2 (c) \
489 + 0xF0]) \
490 && CHAR_FIELD3 (c) >= 32) \
491 : ((c) < MIN_CHAR_PRIVATE_DIMENSION2 \
492 ? (!NILP (XCHAR_TABLE (Vcharset_table)->contents[CHAR_FIELD1 (c) \
493 + 0x10F]) \
494 && CHAR_FIELD2 (c) >= 32 && CHAR_FIELD3 (c) >= 32) \
495 : ((c) < MIN_CHAR_COMPOSITION \
496 ? (!NILP (XCHAR_TABLE (Vcharset_table)->contents[CHAR_FIELD1 (c) \
497 + 0x160]) \
498 && CHAR_FIELD2 (c) >= 32 && CHAR_FIELD3 (c) >= 32) \
499 : (c) < MIN_CHAR_COMPOSITION + n_cmpchars)))
500
feb453fa
RS
501/* The charset of non-ASCII character C is stored in CHARSET, and the
502 position-codes of C are stored in C1 and C2.
503 We store -1 in C2 if the character is just 2 bytes.
504
505 Do not use this macro for an ASCII character. */
506
4ed46869
KH
507#define SPLIT_NON_ASCII_CHAR(c, charset, c1, c2) \
508 ((c) < MIN_CHAR_OFFICIAL_DIMENSION2 \
509 ? (charset = CHAR_FIELD2 (c) + 0x70, \
510 c1 = CHAR_FIELD3 (c), \
d6f92c04 511 c2 = -1) \
4ed46869
KH
512 : (charset = ((c) < MIN_CHAR_COMPOSITION \
513 ? (CHAR_FIELD1 (c) \
514 + ((c) < MIN_CHAR_PRIVATE_DIMENSION2 ? 0x8F : 0xE0)) \
515 : CHARSET_COMPOSITION), \
516 c1 = CHAR_FIELD2 (c), \
517 c2 = CHAR_FIELD3 (c)))
518
feb453fa
RS
519/* The charset of character C is stored in CHARSET, and the
520 position-codes of C are stored in C1 and C2.
521 We store -1 in C2 if the character is just 2 bytes. */
522
4ed46869
KH
523#define SPLIT_CHAR(c, charset, c1, c2) \
524 (SINGLE_BYTE_CHAR_P (c) \
d6f92c04 525 ? charset = CHARSET_ASCII, c1 = (c), c2 = -1 \
4ed46869
KH
526 : SPLIT_NON_ASCII_CHAR (c, charset, c1, c2))
527
feb453fa
RS
528/* The charset of the character at STR is stored in CHARSET, and the
529 position-codes are stored in C1 and C2.
530 We store -1 in C2 if the character is just 2 bytes.
531
4ed46869
KH
532 If the character is a composite character, the upper 7-bit and
533 lower 7-bit of CMPCHAR-ID are set in C1 and C2 respectively. No
534 range checking. */
feb453fa 535
4ed46869
KH
536#define SPLIT_STRING(str, len, charset, c1, c2) \
537 ((BYTES_BY_CHAR_HEAD ((unsigned char) *(str)) < 2 \
538 || BYTES_BY_CHAR_HEAD ((unsigned char) *(str)) > len \
f6469851 539 || split_non_ascii_string (str, len, &charset, &c1, &c2) < 0) \
4ed46869
KH
540 ? c1 = *(str), charset = CHARSET_ASCII \
541 : charset)
542
4ed46869
KH
543/* Mapping table from ISO2022's charset (specified by DIMENSION,
544 CHARS, and FINAL_CHAR) to Emacs' charset. Should be accessed by
545 macro ISO_CHARSET_TABLE (DIMENSION, CHARS, FINAL_CHAR). */
546extern int iso_charset_table[2][2][128];
547
548#define ISO_CHARSET_TABLE(dimension, chars, final_char) \
549 iso_charset_table[XINT (dimension) - 1][XINT (chars) > 94][XINT (final_char)]
550
551#define BASE_LEADING_CODE_P(c) (BYTES_BY_CHAR_HEAD ((unsigned char) (c)) > 1)
552
553/* The following two macros CHAR_STRING and STRING_CHAR are the main
554 entry points to convert between Emacs two types of character
555 representations: multi-byte form and single-word form (character
556 code). */
557
558/* Set STR a pointer to the multi-byte form of the character C. If C
559 is not a composite character, the multi-byte form is set in WORKBUF
560 and STR points WORKBUF. The caller should allocate at least 4-byte
561 area at WORKBUF in advance. Returns the length of the multi-byte
c6112b99 562 form. If C is an invalid character code, signal an error. */
4ed46869
KH
563
564#define CHAR_STRING(c, workbuf, str) \
565 (SINGLE_BYTE_CHAR_P (c) \
566 ? *(str = workbuf) = (unsigned char)(c), 1 \
567 : non_ascii_char_to_string (c, workbuf, &str))
568
569/* Return a character code of the character of which multi-byte form
570 is at STR and the length is LEN. If STR doesn't contain valid
571 multi-byte form, only the first byte in STR is returned. */
572
573#define STRING_CHAR(str, len) \
574 ((BYTES_BY_CHAR_HEAD ((unsigned char) *(str)) == 1 \
575 || BYTES_BY_CHAR_HEAD ((unsigned char) *(str)) > (len)) \
576 ? (unsigned char) *(str) \
577 : string_to_non_ascii_char (str, len, 0))
578
579/* This is like STRING_CHAR but the third arg ACTUAL_LEN is set to
580 the length of the multi-byte form. Just to know the length, use
581 MULTIBYTE_FORM_LENGTH. */
582
583#define STRING_CHAR_AND_LENGTH(str, len, actual_len) \
584 ((BYTES_BY_CHAR_HEAD ((unsigned char) *(str)) == 1 \
585 || BYTES_BY_CHAR_HEAD ((unsigned char) *(str)) > (len)) \
586 ? (actual_len = 1), (unsigned char) *(str) \
587 : string_to_non_ascii_char (str, len, &actual_len))
588
520afed1
RS
589/* Fetch the "next" multibyte character from Lisp string STRING
590 at byte position BYTEIDX, character position CHARIDX.
591 Store it into OUTPUT.
592
593 All the args must be side-effect-free.
594 BYTEIDX and CHARIDX must be lvalues;
595 we increment them past the character fetched. */
596
597#define FETCH_STRING_CHAR_ADVANCE(OUTPUT, STRING, CHARIDX, BYTEIDX) \
598if (1) \
599 { \
600 unsigned char *fetch_string_char_ptr = &XSTRING (STRING)->data[BYTEIDX]; \
601 int fetch_string_char_space_left = XSTRING (STRING)->size_byte - BYTEIDX; \
602 int actual_len; \
603 \
604 OUTPUT \
605 = STRING_CHAR_AND_LENGTH (fetch_string_char_ptr, \
606 fetch_string_char_space_left, actual_len); \
607 \
608 BYTEIDX += actual_len; \
609 CHARIDX++; \
610 } \
611else
612
4ed46869
KH
613/* Return the length of the multi-byte form at string STR of length LEN. */
614
615#define MULTIBYTE_FORM_LENGTH(str, len) \
616 ((BYTES_BY_CHAR_HEAD (*(unsigned char *)(str)) == 1 \
617 || BYTES_BY_CHAR_HEAD (*(unsigned char *)(str)) > (len)) \
618 ? 1 \
619 : multibyte_form_length (str, len))
620
621/* Set C a (possibly multibyte) character at P. P points into a
622 string which is the virtual concatenation of STR1 (which ends at
623 END1) or STR2 (which ends at END2). */
624
625#define GET_CHAR_AFTER_2(c, p, str1, end1, str2, end2) \
626 do { \
627 const char *dtemp = (p) == (end1) ? (str2) : (p); \
628 const char *dlimit = ((p) >= (str1) && (p) < (end1)) ? (end1) : (end2); \
629 c = STRING_CHAR (dtemp, dlimit - dtemp); \
630 } while (0)
631
632/* Set C a (possibly multibyte) character before P. P points into a
633 string which is the virtual concatenation of STR1 (which ends at
634 END1) or STR2 (which ends at END2). */
635
636#define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
637 do { \
638 const char *dtemp = (p); \
639 const char *dlimit = ((p) > (str2) && (p) <= (end2)) ? (str2) : (str1); \
640 while (dtemp-- > dlimit && (unsigned char) *dtemp >= 0xA0); \
641 c = STRING_CHAR (dtemp, p - dtemp); \
642 } while (0)
643
644#ifdef emacs
645
646/* Increase the buffer point POS of the current buffer to the next
647 character boundary. This macro relies on the fact that *GPT_ADDR
648 and *Z_ADDR are always accessible and the values are '\0'. No
649 range checking of POS. */
28a29eb0
KH
650#define INC_POS(pos) \
651 do { \
c399b461 652 unsigned char *p = BYTE_POS_ADDR (pos); \
28a29eb0 653 pos++; \
bb5999ae 654 if (BASE_LEADING_CODE_P (*p++)) \
c399b461 655 while (!CHAR_HEAD_P (*p)) p++, pos++; \
4ed46869
KH
656 } while (0)
657
658/* Decrease the buffer point POS of the current buffer to the previous
659 character boundary. No range checking of POS. */
bb5999ae
KH
660#define DEC_POS(pos) \
661 do { \
662 unsigned char *p, *p_min; \
663 \
664 pos--; \
665 if (pos < GPT_BYTE) \
666 p = BEG_ADDR + pos - 1, p_min = BEG_ADDR; \
667 else \
4ed46869 668 p = BEG_ADDR + GAP_SIZE + pos - 1, p_min = GAP_END_ADDR; \
bb5999ae
KH
669 if (p > p_min && !CHAR_HEAD_P (*p)) \
670 { \
671 int pos_saved = pos--; \
672 p--; \
673 while (p > p_min && !CHAR_HEAD_P (*p)) p--, pos--; \
674 if (!BASE_LEADING_CODE_P (*p)) pos = pos_saved; \
675 } \
c399b461
RS
676 } while (0)
677
678/* Increment both CHARPOS and BYTEPOS, each in the appropriate way. */
679
d619ee06
RS
680#define INC_BOTH(charpos, bytepos) \
681do \
682 { \
683 (charpos)++; \
684 if (NILP (current_buffer->enable_multibyte_characters)) \
685 (bytepos)++; \
686 else \
687 INC_POS ((bytepos)); \
688 } \
c399b461
RS
689while (0)
690
691/* Decrement both CHARPOS and BYTEPOS, each in the appropriate way. */
692
d619ee06
RS
693#define DEC_BOTH(charpos, bytepos) \
694do \
695 { \
696 (charpos)--; \
697 if (NILP (current_buffer->enable_multibyte_characters)) \
698 (bytepos)--; \
699 else \
700 DEC_POS ((bytepos)); \
701 } \
c399b461
RS
702while (0)
703
704/* Increase the buffer point POS of the current buffer to the next
705 character boundary. This macro relies on the fact that *GPT_ADDR
706 and *Z_ADDR are always accessible and the values are '\0'. No
707 range checking of POS. */
bb5999ae
KH
708#define BUF_INC_POS(buf, pos) \
709 do { \
c399b461 710 unsigned char *p = BUF_BYTE_ADDRESS (buf, pos); \
bb5999ae
KH
711 pos++; \
712 if (BASE_LEADING_CODE_P (*p++)) \
713 while (!CHAR_HEAD_P (*p)) p++, pos++; \
c399b461
RS
714 } while (0)
715
716/* Decrease the buffer point POS of the current buffer to the previous
717 character boundary. No range checking of POS. */
718#define BUF_DEC_POS(buf, pos) \
719 do { \
720 unsigned char *p, *p_min; \
721 int pos_saved = --pos; \
722 if (pos < BUF_GPT_BYTE (buf)) \
723 { \
724 p = BUF_BEG_ADDR (buf) + pos - 1; \
725 p_min = BUF_BEG_ADDR (buf); \
726 } \
727 else \
728 { \
729 p = BUF_BEG_ADDR (buf) + BUF_GAP_SIZE (buf) + pos - 1; \
730 p_min = BUF_GAP_END_ADDR (buf); \
731 } \
bb5999ae
KH
732 if (p > p_min && !CHAR_HEAD_P (*p)) \
733 { \
734 int pos_saved = pos--; \
735 p--; \
736 while (p > p_min && !CHAR_HEAD_P (*p)) p--, pos--; \
737 if (!BASE_LEADING_CODE_P (*p)) pos = pos_saved; \
738 } \
4ed46869
KH
739 } while (0)
740
741#endif /* emacs */
742
743/* Maximum counts of components in one composite character. */
744#define MAX_COMPONENT_COUNT 16
745
746/* Structure to hold information of a composite character. */
747struct cmpchar_info {
748 /* Byte length of the composite character. */
749 int len;
750
751 /* Multi-byte form of the composite character. */
752 unsigned char *data;
753
754 /* Length of glyph codes. */
755 int glyph_len;
756
757 /* Width of the overall glyph of the composite character. */
758 int width;
759
760 /* Pointer to an array of glyph codes of the composite character.
761 This actually contains only character code, no face. */
762 GLYPH *glyph;
763
764 /* Pointer to an array of composition rules. The value has the form:
765 (0xA0 + ((GLOBAL-REF-POINT << 2) | NEW-REF-POINT))
766 where each XXX-REF-POINT is 0..8. */
767 unsigned char *cmp_rule;
768
769 /* Pointer to an array of x-axis offset of left edge of glyphs
770 relative to the left of of glyph[0] except for the first element
771 which is the absolute offset from the left edge of overall glyph.
772 The actual pixel offset should be calculated by multiplying each
773 frame's one column width by this value:
774 (i.e. FONT_WIDTH (f->output_data.x->font) * col_offset[N]). */
775 float *col_offset;
776
777 /* Work slot used by `dumpglyphs' (xterm.c). */
778 int face_work;
779};
780
781/* Table of pointers to the structure `cmpchar_info' indexed by
782 CMPCHAR-ID. */
783extern struct cmpchar_info **cmpchar_table;
784/* Number of the current composite characters. */
785extern int n_cmpchars;
786
787/* This is the maximum length of multi-byte form. */
788#define MAX_LENGTH_OF_MULTI_BYTE_FORM (MAX_COMPONENT_COUNT * 6)
789
03e66c53
KH
790/* Maximum character code currently used. */
791#define MAX_CHAR (MIN_CHAR_COMPOSITION + n_cmpchars)
4ed46869 792
bb5999ae
KH
793extern void invalid_character P_ ((int));
794
c04809fb
AS
795extern int unify_char P_ ((Lisp_Object, int, int, int, int));
796extern int split_non_ascii_string P_ ((unsigned char *, int, int *,
797 unsigned char *, unsigned char *));
798extern int string_to_non_ascii_char P_ ((unsigned char *, int, int *));
799extern int non_ascii_char_to_string P_ ((int, unsigned char *, unsigned char **));
800extern int multibyte_form_length P_ ((unsigned char *, int));
801extern int str_cmpchar_id P_ ((unsigned char *, int));
802extern int get_charset_id P_ ((Lisp_Object));
803extern int cmpchar_component P_ ((unsigned int, unsigned int));
804extern int find_charset_in_str P_ ((unsigned char *, int, int *, Lisp_Object));
805extern int strwidth P_ ((unsigned char *, int));
2db95897 806
c1f6608b
KH
807extern Lisp_Object Vcharacter_unification_table_vector;
808#define UNIFICATION_ID_TABLE(id) \
809 (XCONS(XVECTOR(Vcharacter_unification_table_vector)->contents[(id)])->cdr)
810
811/* Copy LEN bytes from FROM to TO. This macro should be used only
812 when a caller knows that LEN is short and the obvious copy loop is
813 faster than calling bcopy which has some overhead. */
814
815#define BCOPY_SHORT(from, to, len) \
816 do { \
817 int i = len; \
818 unsigined char *from_p = from, *to_p = to; \
819 while (i--) *from_p++ = *to_p++; \
820 } while (0)
821
03e66c53 822#endif /* _CHARSET_H */